1

While fixing an validation error preventing user from saving with the message

"You have 100%. You need to have 100%!"

I have ran into very interesting issue happening in both Firefox 34 and IE 11:

0.57+8.36+3.36+0.19 

gives in Firefox and Chrome

12.479999999999999

And in IE

12.479999999999998

If no one wants to calculate, correct result would be 12.48

I will fix issue now by using Math.Round in reduce function to "correct" value on every step, but real question is, what is cause of this at the first look (of naive user) incorrect behavior?

JSFiddle

Note that I have originally discovered issue by trying to sum an Array:

[ 0.57, 8.36, 3.36, 0.19, 39.29, 6.37, 17.24, 0.16, 2.07, 0.04, 22.36].sum()
[ 0.57, 8.36, 3.36, 0.19, 39.29, 6.37, 17.24, 0.16, 2.07, 0.04, 22.36].reduce(function(item,item2){return item+item2})
Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79
  • 1
    Floating-point arithmetic is subject to rounding errors. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Ethan Brown Aug 26 '14 at 23:57
  • thanks, good one, and very important lesson for the team! :) – Goran Obradovic Aug 26 '14 at 23:59
  • Round to a suitable precision when doing comparisons of calculated values (say one significant digit more than you need), or allow +/- to a suitable tolerance. – RobG Aug 27 '14 at 00:01
  • 2
    Interesting -- and unexpected -- that IE (JScript) differs from Chrome (V8) and Firefox (SpiderMonkey). Even IE11: http://jsbin.com/lisina/1 Microsoft seems to be out on their own on this one (quelle shock), Java agrees with V8 and SpiderMonkey. – T.J. Crowder Aug 27 '14 at 00:02
  • @T.J.Crowder—interesting perhaps, but why is that surprising or even remarkable? Is IE non–compliant with either ECMA-262 or IEEE 754? – RobG Aug 27 '14 at 00:17
  • @RobG: In my naïveté, I would have expected MS to be able to implement IEEE-754 double-precision floating point correctly. I don't *know* that they're the ones who got it wrong, of course, because I haven't done the math. But as Mozilla, Google, and Oracle/Sun all agree on a result, let's just say I think it's probably MS who's messed it up... – T.J. Crowder Aug 27 '14 at 05:13
  • @T.J.Crowder—just because IE is different doesn't mean it's wrong, there is likely sufficient ambiguity in the standard for them to both be right. For the record, `12.479999999999998 === 12.479999999999999` returns *true*, so at least as far as IEEE 754 floats are concerned, the values are identical. Try this clj thread: [*IE and IEE 754*](https://groups.google.com/forum/#!topic/comp.lang.javascript/xRJESD4ohfA). – RobG Aug 28 '14 at 03:46

0 Answers0