Decimal comparisons

Kris Zyp kris at sitepen.com
Fri Sep 19 07:25:34 PDT 2008


>> +1 for typeof 1m === 'number'. As an example of breakage, I believe
>> Crockford's current version of his JSON library would not do as I  would
>> desire with decimals:
>>
>> JSON.stringify({foo:1m}) -> "{\"foo\":undefined}"
>
> Why is that worse than producing '{"foo":1}'?

The fact that you know what I was expecting without me saying seems like 
good evidence.

> Consider 1.1m instead  of 1m.

Would be:
'{"foo":1.1}'


> JSON does not provide for decimal, and receiver-makes-it-wrong is a  bug. 
> JSON would need to be extended to handle decimal reliably.

No, JSON only provides for decimal, JSON uses radix 10 to encode numbers, it 
has no support for binary. JSON doesn't need to be extended (unless you want 
to see binary added, but I don't that would be popular). JSON makes no 
assertions about what format a receiver must use to store the numbers it 
receives, it transfers numbers in decimal format, and implementations can 
and should be able to use any format they desire to internally represent it 
based on their needs and expectations. Today implementations do vary in 
which format they choose to use, but there is no restriction preventing 
implementations from using decimal formats for de-serialization of numbers, 
and it seems like it would make sense for JavaScript to do so when decimals 
become available.

Kris 



More information about the Es-discuss mailing list