A directive to solve the JavaScript arithmetic precision issue

David Bruant david.bruant at labri.fr
Mon Aug 15 10:33:38 PDT 2011


I have been watching JSConf talks recently and two of them [1] [2]
mentionned the issue that JavaScript has with numbers. Something than
prevent JS to be a good language to interact with a database. The lack
of 64-bits integers is also mentionned.

Based on the proposal page, ES.next does not address this issue for the
moment while it seems to be a strong concern from the JS community.
Soon, with JS being used for Windows 8 apps, the concern will become
even stronger.

As far as I know, the only strawman that could be used to address the
arithmetic precision issue is value proxies [3]. As a personal opinion,
despite my enthusiasm for object proxies, value proxies scare me a bit.

I don't know if it has been proposed already, but what about a
directive? Code could look like:
"use precise arithmetic"; // choose any other name you'd like here

var a = 0.1 + 0.2; // 0.3 // FINALLY!
What number format should be used? I have no expertise whatsoever in
that domain, so i'll let people who do talk.
In his talks, Douglas Crockford mentions that the fact that JS has one
number type is a good thing, so, maybe that choosing a unique
replacement should be the best thing.
Alternatively, the directive could be "parametrized" with number formats
("use number IEEE XYZ").

A directive would have the same benefits than use strict which is to not
break existing code in platform that do not support this directive. With
a relevant arithmetic test, feature testing will be doable, enabling to
determine whether or not a given platform can execute accurate
arithmetic (and taking action if not).

It would also have the benefit to fix arithmetic for existing script (at
the cost of adding a directive on top).



[1] http://blip.tv/jsconf/nodeconf-2011-nodecommit-panel-edited-5420208
[2] http://blip.tv/jsconf/jsconf2011-andrew-paprocki-5455215
[3] http://wiki.ecmascript.org/doku.php?id=strawman:value_proxies

More information about the es-discuss mailing list