ChakraCore
ChakraCore copied to clipboard
[Bug]: A problem about rounding double precision floating point numbers
ChakraCore Version
36becec
Steps to reproduce
When I use the 'print()' method to output an array containing a specific double-precision floating-point number, there is an unexpected rounding error in the last digit. Should the binary representation of the value be stored accurately?
Proof of concept
const testNumber = 9.758231785433683;
const numberArray = [testNumber];
print(numberArray);
Exception or Error
9.758231785433682
Additional Context
No response
@ayuan0828 this is because of small rounding error. Where in the code we are using the above example and relying on more than 10th precision?
It was discovered during use and I was just a little curious as to why ChakraCore was implemented this way.
This is a duplicate of issue #149, never closed because fixing it proved to be a lot of work for a fairly small gain - cases where the difference matters is very rare. Though I'd love to accept a PR that fixes it.
To answer some curiosity/give background Floating points are stored as a native type that is not displayable as a string; (this is done in all JS engines) because much faster maths can be done with the native Float than a string of digits.
Converting that native type to a display string is actually quite complex see issue #149 for more.