KJS::SimpleNumber on 64 bit machines

Maciej Stachowiak mjs at apple.com
Wed Oct 1 17:11:21 CEST 2003


On Oct 1, 2003, at 3:03 PM, Dirk Mueller wrote:

> On Wednesday 01 October 2003 22:29, Harri Porten wrote:
>
>> Meanwhile I got a bit further. Basic features work but the
>> khtmltests/js/Number.js tests produces segfaults in different places 
>> of
>> dtoa.cpp. Like:
>
> Something I wanted to ask before: whats the reason for this dtoa.cpp 
> file
> being in Javascriptcore at all?

The ECMAScript spec is very specific about how float-to-string 
conversions are supposed to be done. Also, above and beyond what the 
spec says, existing browsers are very careful to make sure to, for 
example, print 1.9 as "1.9" instead of "1.899999999999". Often, you 
can't distinguish these due to the limited precision of floating point, 
and the spec allows either one, but in practice, you need to print the 
shortest of the possible valid representations. There are we sites that 
depend on this behavior.

You can't assume that all printf implementations will get it right. 
However, dtoa.cpp has exactly the right guarantees.

In fact, this specific code base is mentioned in the ECMAScript spec as 
an example of code that meets the requirements.

> Is this a Mac specific problem?

Not as far as I know. I don't know of any printf that can be assumed to 
meet the ECMAScript and real-world requirements. Mozilla also includes 
a copy of this same code for JS number formatting.

Here's a test program you can try on various printf implementations. On 
Mac OS X it prints:

1.89999997615814208984

-------------
int main(int argc, char **argv)
{
     printf("%.20f\n", (float)1.9);
}
------------


Regards,
Maciej



More information about the Khtml-devel mailing list