@zachleat@fediverse.zachleat.com
2026-04-16 15:38 UTC
JavaScript addition using the new and upcoming Math.sumPrecise():
❌ 1 + 0.1 - 1 = 0.10000000000000009
✅ Math.sumPrecise([1,.1,-1]) = 0.1
❌ 1e20 + 0.1 - 1e20 = 0
✅ Math.sumPrecise([1e20,.1,-1e20]) = 0.1
❌ 0.1 + 0.2 = 0.30000000000000004
❌ Math.sumPrecise([.1,.2]) = 0.30000000000000004
Read more on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sumPrecise
Replies (5)
-
@AmeliaBR@front-end.social 2026-04-16 15:44
@zachleat Coming soon to a JS API near you: Math.sumPreciserFinalv2UseThisOne()
-
@ardouglass@mastodon.social 2026-04-16 16:01
@zachleat I love that due to backwards compatibility we can’t simply fix the plus sign. Some library out there (probably MooTools) certainly used floating point math errors in a load bearing way that would cause the web to fall apart if changed.
-
@ardouglass@mastodon.social 2026-04-16 16:07
@zachleat wait… “This still cannot avoid the 0.1 + 0.2 precision problem”
-
@chriskirknielsen@front-end.social 2026-04-16 16:17
@zachleat Dang I thought the last scenario would be solved by this. 😬 Should have been called `sumLossless`, given that it is not actually precise for that typical `.1+.2` example. Ah well, what can you do!
-
@mottokrosh@social.mottokrosh.com 2026-04-17 14:34
@zachleat Math.sumVibe() when?