Possible Duplicate:
Strange floating-point behaviour in a Java program
I came across this weird phenomenon in Java. Try this statement in a Java program:
System.out.print(4.0-3.1);
The output will be 0.8999999
Why does this happen? And how can it be changed?
This is a typical floating point result runded.
You get different results from
FloatandDouble:Output:
This is because
0.1cannot be represented evenly in base 2, and cause a loss of precision. For example :Should both return
0.1but in fact will output :