Re: Strange results when adding two double primitives - Java 1.5.0_04
Chris Brat wrote:
Hi,
I've found this little oddity in the application I'm working on where
the result of adding two double primitives gives a result that either
has an extra 0.0000000000000002 or it is 0.0000000000000001 less than
the expected result.
No matter what radix is used, there will be some rationals that cannot
be represented exactly as an integer scaled by a power of the radix,
leading to rounding error.
For many applications, the radix does not matter to the application. For
example, some applications do calculations whose infinitely precise
result is irrational, and, short of symbolic math systems, rounding
error is inevitable. Two is the best radix for very compact, high
performance hardware implementation.
However, it is also a reality that, for some applications, those numbers
that are exactly representable with radix 10 are particularly important.
That is why Java provides the class java.math.BigDecimal. It gives up
some of the efficiency of double, but gains exact representation of any
decimal fraction. Here is your program, converted to use BigDecimal:
import java.math.BigDecimal;
public class BigDecimalTest {
public static void main(String[] args) {
// double a = 67.41;
BigDecimal a = new BigDecimal("67.41");
// double b = 51.85;
BigDecimal b = new BigDecimal("51.85");
// double result = a + b;
BigDecimal result = a.add(b);
//
// // I get 119.25999999999999
System.out.println(result);
//
//
//
// a = 1.01;
a = new BigDecimal("1.01");
// b = 2.02;
b = new BigDecimal("2.02");
// result = a + b;
result = a.add(b);
//
// // I get 3.0300000000000002
System.out.println(result);
//
// a = 1.100;
a = new BigDecimal("1.100");
// b = 2.103;
b = new BigDecimal("2.103");
//
// result = a + b;
result = a.add(b);
// // 3.2030000000000003
System.out.println(result);
}
}
Output:
119.26
3.03
3.203
Note that, in this particular case, you could have got the ideal output
by just using DecimalFormat or System.out.printf to print the doubles
rounded to a reasonable precision. However, with more significant digits
in the inputs, or a lot more steps in the calculations, the rounding
error could affect digits that matter.
Patricia