Why decimal?

Christian Plesner Hansen christian.plesner.hansen at gmail.com
Tue Jun 23 00:18:16 PDT 2009


I've been looking around on the web for reasons why decimal arithmetic
should be added to ES.  The most extensive page I could find was
http://speleotrove.com/decimal/decifaq.html.  If anyone know other
good sources of information about decimal and its inclusion in ES
please follow up.

The strongest argument seems to be financial: binary arithmetic is
approximate in a way that makes it unsuitable for financial
calculations.  I buy this argument in general -- I would definitely
want my bank to use some form of decimal arithmetic rather than binary
-- but less so in the context of ES.  A while ago we (the v8 team)
asked around within google and were unable to find anyone who would
want to do financial calculations client-side.  Financial calculations
are sensitive and keeping it on the server makes it possible to
maintain consistency, accountability, testability and reproducibility.
 So while I'm all for decimal in languages that run server side it's
much less relevant for a language that runs almost exclusively on the
client.

Another data point is that the one decimal library that exists for JS,
a direct translation from Java's BigDecimal, hasn't been updated since
2000, during which time a number of bugs have been fixed in Java
BigDecimal.  If there had been a broad interest in using a JS decimal
library I would expect there to be a well-maintained one around.

I found a handful of blog posts supporting decimal from an accuracy
standpoint -- essentially "0.1 + 0.2 should be equal to 0.3".  The
argument looks compelling but is somewhat misleading.  All floating
point arithmetic is approximate.  K-bit decimal arithmetic trades
smaller but conspicuous inaccuracies for greater but less conspicuous
ones when compared with k-bit binary.  In some contexts, financial
again for instance, the accuracy argument makes sense but not as a
general statement without qualification.  Nonetheless, far as I can
tell this is the source of a large portion of the requests for
supporting decimal.  Furthermore, decimal itself is counter-intuitive
in a number of ways: the difference between 1e2 and 100, for instance,
is counter-intuitive in many contexts.  I know the most obvious
differences have been removed from the ES proposal; I'm not sure if
there are less obvious differences left (like with +/-0).

Another problem is that the standard is closed: I can't even read it
without paying for it. Granted, that also holds for the specification
of double but with double there is universal native support, we don't
have to maintain an implementation ourselves.  That's not the case for
decimal.  We as implementers of the language would have to potentially
create or at least maintain an implementation of a closed standard.

Finally, the usefulness of decimal either a native implementation or a
library would have to be weighed against the increase in the size of
the implementation.  The fact that JS is becoming increasingly popular
on limited devices speaks against adding a large decimal library.


-- Christian


More information about the es-discuss mailing list