Why is a round-trip conversion via a string not safe for a double?

I found the bug. .NET does the following in clr\src\vm\comnumber.cpp: DoubleToNumber(value, DOUBLE_PRECISION, &number); if (number.scale == (int) SCALE_NAN) { gc.refRetVal = gc.numfmt->sNaN; goto lExit; } if (number.scale == SCALE_INF) { gc.refRetVal = (number.sign? gc.numfmt->sNegativeInfinity: gc.numfmt->sPositiveInfinity); goto lExit; } NumberToDouble(&number, &dTest); if (dTest == value) { gc.refRetVal = NumberToString(&number, ‘G’, DOUBLE_PRECISION, gc.numfmt); goto lExit; } DoubleToNumber(value, … Read more

Double cannot be dereferenced?

EDIT 4/23/12 double cannot be dereferenced is the error some Java compilers give when you try to call a method on a primitive. It seems to me double has no such method would be more helpful, but what do I know. From your code, it seems you think you can copy a text representation of … Read more

Should we generally use float literals for floats instead of the simpler double literals?

Yes, you should use the f suffix. Reasons include: Performance. When you write float foo(float x) { return x*3.14; }, you force the compiler to emit code that converts x to double, then does the multiplication, then converts the result back to single. If you add the f suffix, then both conversions are eliminated. On … Read more

Conversion of a decimal to double number in C# results in a difference

Interesting – although I generally don’t trust normal ways of writing out floating point values when you’re interested in the exact results. Here’s a slightly simpler demonstration, using DoubleConverter.cs which I’ve used a few times before. using System; class Test { static void Main() { decimal dcm1 = 8224055000.0000000000m; decimal dcm2 = 8224055000m; double dbl1 … Read more

Scanf/Printf double variable C

For variable argument functions like printf and scanf, the arguments are promoted, for example, any smaller integer types are promoted to int, float is promoted to double. scanf takes parameters of pointers, so the promotion rule takes no effect. It must use %f for float* and %lf for double*. printf will never see a float … Read more

Comparator with double type

I suggest you use the builtin method Double.compare(). If you need a range for double values to be equal you can use chcek for that first. return Double.compare(p1.getY(), p2.gety()); or if(Math.abs(p1.getY()-p2.getY()) < ERR) return 0; return Double.compare(p1.getY(), p2.gety()); The problem with using < and > is that NaN will return false in both cases resulting … Read more

What is the inclusive range of float and double in Java?

Java’s Primitive Data Types boolean: 1-bit. May take on the values true and false only. byte: 1 signed byte (two’s complement). Covers values from -128 to 127. short: 2 bytes, signed (two’s complement), -32,768 to 32,767 int: 4 bytes, signed (two’s complement). -2,147,483,648 to 2,147,483,647. long: 8 bytes signed (two’s complement). Ranges from -9,223,372,036,854,775,808 to … Read more

tech