40

So when I add or subtract in Java with Doubles, it giving me strange results. Here are some:

If I add 0.0 + 5.1, it gives me 5.1. That's correct.

If I add 5.1 + 0.1, it gives me 5.199999999999 (The number of repeating 9s may be off). That's wrong.

If I subtract 4.8 - 0.4, it gives me 4.39999999999995 (Again, the repeating 9s may be off). That's wrong.

At first I thought this was only the problem with adding doubles with decimal values, but I was wrong. The following worked fine:

5.1 + 0.2 = 5.3
5.1 - 0.3 = 4.8

Now, the first number added is a double saved as a variable, though the second variable grabs the text from a JTextField. For example:

//doubleNum = 5.1 RIGHT HERE
//The textfield has only a "0.1" in it.
doubleNum += Double.parseDouble(textField.getText());
//doubleNum = 5.199999999999999
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
  • btw, prefer using BigDecimal (http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html ) – user2147970 Mar 25 '13 at 21:59
  • Actually, 5.19999999... (with infinitely many repeating 9's) is mathematically exactly the same as 5.2, if you don't believe it, try to find out what the difference between the two is. It is 0, hence, they are the same. – Ingo Mar 25 '13 at 22:06
  • 1
    @Ingo technically, yes, but I would like to have a little bit precise answer :) – Rob Avery IV Mar 25 '13 at 22:12
  • Rob, I understand, and I find it very regrettable that those numbers are known as "float" and "double". Appropriate names would be "coarse approximations" and "slightly better, but still coarse approximations" – Ingo Mar 25 '13 at 23:04

2 Answers2

38

In Java, double values are IEEE floating point numbers. Unless they are a power of 2 (or sums of powers of 2, e.g. 1/8 + 1/4 = 3/8), they cannot be represented exactly, even if they have high precision. Some floating point operations will compound the round-off error present in these floating point numbers. In cases you've described above, the floating-point errors have become significant enough to show up in the output.

It doesn't matter what the source of the number is, whether it's parsing a string from a JTextField or specifying a double literal -- the problem is inherit in floating-point representation.

Workarounds:

  • If you know you'll only have so many decimal points, then use integer arithmetic, then convert to a decimal:

    (double) (51 + 1) / 10
    (double) (48 - 4) / 10
    
  • Use BigDecimal

  • If you must use double, you can cut down on floating-point errors with the Kahan Summation Algorithm.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

In Java, doubles use IEEE 754 floating point arithmetic (see this Wikipedia article), which is inherently inaccurate. Use BigDecimal for perfect decimal precision. To round in printing, accepting merely "pretty good" accuracy, use printf("%.3f", x).

raptortech97
  • 507
  • 3
  • 14