0

I'm currently investigating a bug in our system which is related to rounding errors. I just discovered that Math.floor() produces different results when using an operation as argument - e.g.:

Math.floor(1681); // produces 1681
Math.floor(16.81 * 100) // produces 1680
Math.floor(16.811 * 100) // produces 1681

Can anyone explain why? This is hurting my brain.

Michel
  • 303
  • 2
  • 15
  • 1
    Because of the way binary floating point numbers work, 16.81 cannot be represented exactly, and so 16.81 * 100 does *not* produce 1681, it produces 1680.9999999999998. Take the floor of that and you get 1680. For more info, see the answers to the linked question – Nicholas Tower Jan 23 '21 at 21:01
  • 1
    To see your particular problem in action, open developer tools and type 16.81*100 in to your console - the value I get back is 1680.9999999999998, which is 1680 when floored. – DewiW Jan 23 '21 at 21:03

0 Answers0