2

Possible Duplicate:
Floating point arithmetic not producing exact results in Java
Floating point inaccuracy examples

In Java, given the following code:

    double amount = scan.nextDouble();

    tenBills = (int)amount / (int)10;
    amount = amount - (tenBills * 10);

    fiveBills = (int)amount / (int)5;
    amount = amount - (fiveBills * 5);

After the first calculation, given an input of say 16 amount will equal 6.66 . But after the second calculation amount will be 1.6600000000000001 . I don't understand why subtracting an int from a double would cause such a result.

Community
  • 1
  • 1
user1086516
  • 887
  • 3
  • 9
  • 21
  • 2
    It's called [floating-point arithmetic](http://en.wikipedia.org/wiki/Floating-point_arithmetic) and this question is asked **constantly** on SO. Read this: http://csharpindepth.com/Articles/General/FloatingPoint.aspx – Matt Ball Dec 25 '11 at 01:24
  • After you've got the answer to your direct question you might want to use the [BigDecimal](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) class in your code if you need better precision. – joschi Dec 25 '11 at 01:40

2 Answers2

2

If you want a hardcore explanation, read the classic What Every Computer Scientist Should Know About Floating-Point Arithmetic. It explains why there are inevitably tiny rounding errors in floating point arithmetic like the one you're seeing.

If you just want a summary: Computers represent everything in binary. Binary representations while doing floating point arithmetic results in tiny inaccuracies in some situations.

Trott
  • 66,479
  • 23
  • 173
  • 212
1

Subtracting an int from a double does not change only the integer part of the double. It can also change the scale. In your particular case, the integer part before the subtraction (6) requires more bits to represent than the integer part after subtraction (1). This causes the entire representation of the number to allow for more fractional bits. Results can be (as you found out) a bit unexpected. (Pun intended)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521