Possible Duplicate:
Retain precision with Doubles in java
Do you know the difference between these two operations in Java.
final double m1 = 11d / 1e9; // gives 1.1e-8
final double m2 = 11d * 1e-9; // gives 1.1000000000000001e-8
I see in the generated bytecode that the precompiled result of m2 is already not what I expected.
In the output of javap -verbose -c I can see the following value :
const #3 = double 1.1E-8d;
[...]
const #6 = double 1.1000000000000001E-8d;
When I use m1 or m2 in other expressions, I don’t have the same result.
When I try the same thing in C, m1 and m2 is strictly 1.1e-8
I think my problem is in the way java handles double precision computation but I can’t explain myself what I miss.
The difference you have is that 1e9 is exactly representable and 1e-9 which is not exactly representable. A small representation error results in an arithmetic error which you can see.
prints
The difference is not whether you have used
*or/but that you have used a whole number in one case and a fraction in the other.prints