goodman38
2009-03-24 20:19:10 UTC
Under Windows XP, a Java program that multiplies the number 3.4 three times (such as 3.4*3.4*3.4) produces a product of 39.303999999999995 instead of the correct 39.304. This means that the JDK has a bug, doesn’t it? Can I get the correct result?
The answer I got is this:
"Certainly not. See: http://docs.sun.com/source/806-3568/ncg_goldberg.html
Define ‘correct’. It’s floating-point, not decimal. You can get whatever number of decimal digits and rounding you like with DecimalFormat or java.math.BigDecimal."
I completely disagree with that answer. Reason: all calculators show a product of 39.304; a C++ program shows 39.304; even when I do it by hand, I get 39.304. The only thing that doesn’t show 39.304 is the Java program. 3.4x3.4x3.4 doesn’t require any rounding at all—you get a clear cut number of 39.304. In science, we try to be as accurate as possible. What is your opinion?