0

My current method of rounding to x decimal places:

public static float roundInc(float value, float increment) {
    float inc = 1f / increment;
    return Math.round(value * inc) / inc;
}

Which works fine for certain numbers, but seems to break, and I can't seem to figure this out.

I pass through the value 2.109375 Output of the function returns 2.11 Then I add that to 5.0 (is also a float, stored in a float array (float[])), and for some bizarre reason, adding the 2.11 + 5.0 returns 7.1099997...

Can someone explain this? Cause as far as I can see, after printing the two 2.11, and 5.0, showing those two exact values, then added together breaks everything...

  • For other ways to round a floating point number in Java, see http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java?rq=1 – Thilo Apr 06 '16 at 01:12
  • `float`s are the wrong type for this. Only `BigDecimal` will do this correctly. – Louis Wasserman Apr 06 '16 at 01:14
  • Using BigDecimal's still causes the same problem, as do double's. Also found Python has the same problem even just printing "2.11 + 5.0" – SolarEntropy Apr 06 '16 at 01:26
  • 1
    Everyone please go read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Jim Garrison Apr 06 '16 at 01:50
  • Thanks for that @JimGarrison, definitely clears a lot up on the matter. – SolarEntropy Apr 06 '16 at 01:55
  • @SolarEntropy you must be a fast reader. There's enough detail there to keep someone busy for at least an hour :-) – Jim Garrison Apr 06 '16 at 01:56
  • @JimGarrison I got the gist of it after a quick skim, also Thilo marked this as a duplicate, and the one it was marked a dupe of also iterated similar points. – SolarEntropy Apr 06 '16 at 08:59

0 Answers0