Revisiting Decimal

Sam Ruby rubys at
Thu Jan 15 03:50:53 PST 2009

Kris Zyp wrote:
>>> Only if never compared to a double. How do you prevent this?
> We already agree that the decimal-double comparison will always be
> false.

Not strictly true.

(1m == 1) => true
(1m === 1) => false

It is only fractions which have denominators which are not a pure power 
of two within the precision of double precision floating point that will 
compare unequal.  In particular,

(1.75m == 1.75m) => true
(1.76m == 1.76m) => false

For most people, what that works out to mean is that integers compare 
equal, but fractions almost never do.  It is worth noting that comparing 
fractions that are the result of computations with double precision 
floating point for strict equality rarely works out in practice, one 
typically needs to take into account an "epsilon".

 > The point is that this is representative of real world code
 > that benefits more from the treatment of decimals as numbers.

I agree with your overall argument that the real point of JSON is 
inter-language interoperability, and that when viewed from that 
perspective, and that any JSON support that goes into ECMAScript should 
interpret literals which contain a decimal point as decimal.  But that's 
just an opinion.

At the moment, the present state is that we have discussed at length 
what the results of typeof(1m) and what JSON.parse('[1.1]') should 
return.  And now we are revisiting both without any new evidence.

In the past, I have provided a working implementation, either as a 
standalone JSON interpreter, as a web service, or integrated into 
Firefox.  I could do so again, and provide multiple versions that differ 
only in how they deal with typeof and JSON.parse.

But first, we need to collectively decide what empirical tests would 
help us to make a different conclusion.

- Sam Ruby

More information about the Es-discuss mailing list