Double subtraction precision issue

double is internally stored as a fraction in binary — like 1/4 + 1/8 + 1/16 + ...

The value 0.005 — or the value 1.435 — cannot be stored as an exact fraction in binary, so double cannot store the exact value 0.005, and the subtracted value isn’t quite exact.

If you care about precise decimal arithmetic, use BigDecimal.

You may also find this article useful reading.

Leave a Comment

tech