JavaScript's Tricky Rounding

1 · · April 24, 2018, 7 a.m.
JavaScript rounds in a tricky way. It tricked all the engines, and even itself. Math.round() behaves the same as C’s familiar round with one key difference: it rounds halfways (“is biased”) towards positive infinity. Here is its spec in ES 5.1. It suggests an implementation too: The value of Math.round(x) is the same as the value of Math.floor(x+0.5)... And so every engine did that. Unfortunately, the implementation in the spec does not correctly implement the spec. One bug is that adding 0.5 ...