Comparing floats and doubles in Java

Viewed 883

The following code uses the values of type float and double respectively.

float a=99999.99F;
double b=a;

System.out.println("a : "+a);
System.out.println("b : "+b);
System.out.println("a==b : "+(a==b));

It displays the following output:

a : 99999.99
b : 99999.9921875
a==b : true

After execution, the values of a and b are 99999.99 and 99999.9921875 respectively though a==b returns true.

How does the comparison expression a==b return true?

4 Answers
Related