IEEE 754 float numbers are discret.
public class MyTest2 {
public static void main(String[] args) {
//about 1.00000001490116119384765625E-1 in IEEE-754
float f = 0.1f;
//about 1.00000000000000005551115123126E-1 in IEEE-754
double d = 0.1d;
System.out.println(String.format("double 0.1= %.30f", d));
System.out.println(String.format("float 0.1 = %.15f", f));
System.out.println(d+"");
}
}
See this code run live at IdeOne.com. Running in JDK8, output is
double 0.1= 0.100000000000000000000000000000
float 0.1 = 0.100000001490116
0.1
The float value is printed as expected. I expect double value 0.1d to be printed something like 1.000000000000000055511151231260. Why it print all zeros in fraction part?
If I convert double variable d to string, it prints 0.1.
System.out.println(d+"");
How does java convet the neartest float value of 0.1d (which is stroed as about 1.00000001490116119384765625E-1) to exact 0.1?