Float and double in Java "inaccurate" result

There is a well known problem with precise floating point numbers representation in a computer, and Java is no different in this case. If you want to see it for yourself, than try executing the below code: Double d = new Double(4.395f); System.out.println(d); You might expect the 4.395 will be printed out, right? Well, you’re defining an “exact” number as a float (32 bit), than casting it to the double (64 bit) so there should be no problem, as it shouldn’t affect the value. After all it’s only 4.395 and not 4.3956312123540604, right? In the matter of fact, the result will be like this: ...

February 4, 2011 · 3 min