Revisiting Decimal (generic algorithms)

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Fri Jan 16 17:30:19 PST 2009


David-Sarah Hopwood wrote:
> Allen Wirfs-Brock wrote:
> [...]
>> function fuzz(a) { return a + 0.1}
>> //not generic in Kona draft, fuzz(1m) ===> 1.10000000000000008881784197001... (but see below)
>> //Kona spec. uses binary floating point for all mixed mode operations
>>
> [...]
>> This problem cannot be fixed simply by tweaking the coercion rules.
>> It probably requires that numeric literals be treated as generic values
>> that are only interpreted situationally as either binary or decimal values
>> in the context of a particular operations.
> 
> I am not aware of any precedent for this approach in other languages, and
> I'm very skeptical about whether it can be made to work in ECMAScript.
> Consider
> 
>   function id(x) { return x; }
> 
> What is the result and type of id(0.1) in this approach, and why?

 - if binary 0.1, then we would have

     1m + 0.1 !== 1m + id(0.1)

   which breaks referential transparency (in the absence of side-effects)

 - if decimal 0.1m, then we break compatibility with ES3.

 - if the value remains generic, then such values must be supported at
   run-time as a third numeric type besides number and decimal, which
   seems unsupportably complex to me.

-- 
David-Sarah Hopwood ⚥



More information about the Es-discuss mailing list