This JavaScript demonstrates a bug in IE that is driving me nuts right now:
var y = 6044629098073143; // this exact integer easily fits into an IEEE double
document.write(y + " " + Math.round(y)+"<br><br>");
The output in IE 8 (and Opera 12.02) shows that Math.round is off by 1:
6044629098073143 6044629098073144
Output in Firefox, Chrome and Safari is correct.
What on earth is going on here with IE and Opera?
I’ve confirmed RobG’s comments: All integers below 2^52 (4503599627370496) appear to round correctly in all browsers. Integers higher than this round UP to an EVEN number in IE/Opera with Math.round (whereas other browsers round correctly).
As RobG mentioned, it is likely that IE and Opera implement Math.round(x) as Math.floor(x + 0.5), which leads to these results since adding 0.5 to a value > 2^52 yields an imprecise result. A smarter implementation of round() would use the FPU’s native rounding support (IE and Opera developers, please take note and fix!)
In the meantime, this workaround should work: