Blog

It's a Wonderful Life

Adding two double values in Java

2019-05-09 22:43 Posted in Learn with Java

Today as I was doing modular tests, this is what result I got

modular test

What happens here is that, 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. this is a problem that is inherited 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

  • cut down on floating-point errors with the Kahan Summation Algorithm.