Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A question about floating number #6383

Closed
YuanWangC opened this issue Mar 9, 2020 · 4 comments
Closed

A question about floating number #6383

YuanWangC opened this issue Mar 9, 2020 · 4 comments

Comments

@YuanWangC
Copy link

When I execute the testcase below, chakra gives a different output which is not the same as other engines (like V8, spiderMonkey,javascriptCore). I wonder if chakra has any special ways to deal with floating numbers.

version

eaaf7ac

testcase

var NISLFuzzingFunc = function(){
    print(32.963645576628462);
};
NISLFuzzingFunc();

output

32.963645576628465

expected output

32.963645576628466

@rhuanjl
Copy link
Collaborator

rhuanjl commented Mar 9, 2020

This is due to the algorithm used for converting a floating point number to a string.

Last time I looked at this the specifics of the algorithm were not specified and so CC's behaviour was not violating spec.

The other 3 engines all use forks of the same library (which can be seen here: https://github.com/google/double-conversion) for handling this conversion and hence get the same result.

There is prior discussion in issue #149

There was a possible quick fix to bring the behaviour in line but it had a significant performance cost so was rejected. To bring the behaviour inline without the performance cost would involve either using that library I've linked above OR some more extensive work to significantly re-write the algorithm currently used.

@rhuanjl
Copy link
Collaborator

rhuanjl commented Mar 25, 2020

Closing as duplicate as #149

@rhuanjl rhuanjl closed this as completed Mar 25, 2020
@YiWen-y
Copy link

YiWen-y commented Jun 12, 2020

Version:

chakra-1.11.8

Testcase:

var NISLFuzzingFunc =function (nislMutationParameter0){
    print(Math.pow(nislMutationParameter0,1/3));
};
var nislMutationArgument0=52356; 
NISLFuzzingFunc(nislMutationArgument0); 

Command:

./ChakraCore/out/Debug/ch testcase.js

Output:

37.41009574268063

Expected output:

37.41009574268064

Description:

I manually calculated 52356^(1/3) and the result is 37.410095742680642522405335528085...‬, but the output of the engine is 37.41009574268063. What is the reason for this?

@rhuanjl
Copy link
Collaborator

rhuanjl commented Jun 12, 2020

It is to do with the process for converting from a floating point number to a string you can actually display.

A float is stored internally as described here: https://en.wikipedia.org/wiki/Double-precision_floating-point_format

The method for doing this conversion is not simple AND can never be completely exact. The question is how precise can you manage - being out by 1 on the 16th decimal place is good enough for the majority of uses - though as above it would be good to update to be in sync with the other engines, which is what issue #149 is about.

NOTE - as this is only a conversion for output question - if you do further sums on the number within JS it is is not being truncated or rounded incorrectly until the moment you display it which is why this is not a major issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants