An error occurred when using scientific notation
When I use scientific notation to assign a value to a variable, I find that in some specific cases, Jerry introduces errors, causing subsequent operations to fail.
The common feature of these specific numbers is that the 5th decimal place is odd and the exponent is 21, just like 1.00001e+21, 1.0003e+21, etc. When the assignment is over, the value of the variable introduces a small error.
For example:
var num = 1.00517e+21;
print(num);
// print(String(num));
The result of the program running is 1.0051700000000001e+21.
A similar operation has no errors on nodejs, v8, or SpiderMonkey.
After debugging analysis, it is found that the numerical value literal has been introduced into the error after being processed by the ecma_utf8_string_to_number() function.
I think there is a problem here, please fix it or or give a relevant explanation, thank you.
Are there any relevant explanations on this issue?
Actually, what you are experiencing is not related to scientific notation. E.g., you can write the same number as an integer literal, and you'll see the same behaviour:
jerry> 1005170000000000000000
1.0051700000000001e+21
Those numbers that cannot be represented in an integer are stored as an IEEE 754 double-precision floating point number by JerryScript (i.e., as a C double). A property of that number format is that it cannot represent all decimal numbers exactly. Some background on the format can be found at Wikipedia
I've conducted some tests with various tools to dig a bit deeper.
Output from binaryconvert.com
This is an online tool that shows the (possible) representation of floating point numbers. This tool gives that the double-precision representation of 1.00517e+21 and 1.0051700000000001e+21 is the same.
1.00517e+21
Most accurate representation = 1.005170000000000065536E21
0x444B3EC49AB0C7B3 = 01000100 01001011 00111110 11000100 10011010 10110000 11000111 10110011
1.0051700000000001e+21
Most accurate representation = 1.005170000000000065536E21
0x444B3EC49AB0C7B3 = 01000100 01001011 00111110 11000100 10011010 10110000 11000111 10110011
Output from jerry
Further tests with JerryScript show that both (all) representations of the number are considered equivalent.
jerry> 1.00517e+21 == 1.0051700000000000e+21
true
jerry> 1.0051700000000000e+21 == 1.0051700000000001e+21
true
jerry> 1.00517e+21 == 1.0051700000000001e+21
true
Output from a C program
I've also rewritten the above test to C:
#include <stdio.h>
#define PRINT_NUM(A) printf("%s = %e = %.16e = %f [repr: 0x%08lx]\n", #A, A, A, A, *(unsigned long int*)&A)
#define PRINT_EQ(A,B) printf("%s == %s: %s\n", #A, #B, A == B ? "true" : "false")
int main() {
double num1 = 1.00517e+21;
double num2 = 1.0051700000000000e+21;
double num3 = 1.0051700000000001e+21;
PRINT_NUM(num1);
PRINT_NUM(num2);
PRINT_NUM(num3);
PRINT_EQ(num1, num2);
PRINT_EQ(num1, num3);
PRINT_EQ(num2, num3);
return 0;
}
This, however, shows that the C compiler has chosen different representations for 1.00517e+21 and 1.0051700000000001e+21:
num1 = 1.005170e+21 = 1.0051699999999999e+21 = 1005169999999999934464.000000 [repr: 0x444b3ec49ab0c7b2]
num2 = 1.005170e+21 = 1.0051699999999999e+21 = 1005169999999999934464.000000 [repr: 0x444b3ec49ab0c7b2]
num3 = 1.005170e+21 = 1.0051700000000001e+21 = 1005170000000000065536.000000 [repr: 0x444b3ec49ab0c7b3]
num1 == num2: true
num1 == num3: false
num2 == num3: false
The difference is at the least significant bit of the representation. Note that 1005169999999999934464 (repr 0x444b3ec49ab0c7b2) is just as far away from 1005170000000000000000 (the originally intended number) as 1005170000000000065536 (repr 0x444b3ec49ab0c7b3) is. So, I'm not sure whether all C implementations must choose 0x444b3ec49ab0c7b2 to represent 1.005170e+21 or is that an implementation/rounding detail.
More output from jerry
Finally, I've tested some requirements of the IEEE 754 double-precision representation (quotes from Wikipedia):
If a decimal string with at most 15 significant digits is converted to IEEE 754 double-precision representation, and then converted back to a decimal string with the same number of digits, the final result should match the original string.
jerry> 1.00517e+21.toPrecision(15)
1.00517000000000e+21
jerry> Number(1.00517e+21).toPrecision(15)
1.00517000000000e+21
jerry> Number("1.00517e+21").toPrecision(15)
1.00517000000000e+21
If an IEEE 754 double-precision number is converted to a decimal string with at least 17 significant digits, and then converted back to double-precision representation, the final result must match the original number.
jerry> Number(1.00517e+21.toPrecision(17)) == 1.00517e+21
true
jerry> Number(1.0051700000000000e+21.toPrecision(17)) == 1.0051700000000000e+21
true
jerry> Number(1.0051700000000001e+21.toPrecision(17)) == 1.0051700000000001e+21
true
So, it seems that JerryScript fulfils these requirements.
More output from C
For the sake of completeness, the same test has also been performed with a C program:
#include <stdio.h>
void test1(const char *str) {
double num;
sscanf(str, "%lf", &num);
printf("%s -> %.15e\n", str, num);
}
void test2(double num) {
char str[32];
double _num;
sprintf(str, "%.17e", num);
sscanf(str, "%lf", &_num);
printf("%e [repr: 0x%08lx] -> %s -> %e [repr: 0x%08lx], %s\n", num, *(unsigned long int*)&num, str, _num, *(unsigned long int*)&_num, num == _num ? "equal" : "not equal");
}
int main() {
test1("1.005170000000000e+21");
test2(1.00517e+21);
test2(1.0051700000000001e+21);
return 0;
}
1.005170000000000e+21 -> 1.005170000000000e+21
1.005170e+21 [repr: 0x444b3ec49ab0c7b2] -> 1.00516999999999993e+21 -> 1.005170e+21 [repr: 0x444b3ec49ab0c7b2], equal
1.005170e+21 [repr: 0x444b3ec49ab0c7b3] -> 1.00517000000000007e+21 -> 1.005170e+21 [repr: 0x444b3ec49ab0c7b3], equal
Conclusion?
Not that much. As far as I'm concerned, JerryScript is IEEE 754 compliant. Even if it doesn't work exactly as v8 or SpiderMonkey.
@amapleaf Are you OK with the explanation?
Considering the question as answered, closing the issue. @amapleaf feel free to reopen this issue or open a new one if you feel that this hasn't been resolved properly.
@akosthekiss Sorry for not responding in time, and thank you for your analysis. I think you are right, maybe node and C have done some rounding on this. If I find something about it later, I will write it here, thank you very much for your work.