-
-
Notifications
You must be signed in to change notification settings - Fork 670
fix: prevent powi opt in js engine only when exponent <= -2 #2920
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
base: main
Are you sure you want to change the base?
Conversation
b404098
to
4d9b05b
Compare
4d9b05b
to
a022591
Compare
Hmm, but how about of rest browsers? AS may run in different browsers. Besides, there is no guarantee that even in the next versions of node this speculation will not appear again |
Is the current behavior causing any problems? |
But it still can be slightly optimized I gues to: if (Math.trunc(y) == y && Math.abs(y) > 2 && Math.abs(y) < 100) {
...
}
return Math.pow(x, y); |
In latest version, it will get this result x = 2
y = 2
Math.pow(x, y - 0.5) * Math.pow(x, 0.5) // 4.000000000000001 It the reason why #2917 CI failed |
It is meaningless actually, change IMO, It is impossible to keep determinism between different js engine except introducing high precision pow lib. |
If there's still a (potential) need for this function, is there any other working alternative? |
according to https://tc39.es/ecma262/#sec-numeric-types-number-exponentiate. Math.pow only required to be precision as much as possible. To be honest, I think it is hard to make everything same except not using IEEE754 floating number (software implement). |
I'm also tempted to say "throw this function out", because I can also see this imprecise behavior with integer exponents on Python and Java (tested via |
The best way I see, and the way I originally thought, is to compile the |
What I was thinking: shouldn't we fix our Even C exhibits this behavior: #include <math.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("pi: %e\n", M_PI);
double regular = pow(M_PI, 210);
double accurate = pow(M_PI, 209.5) * pow(M_PI, 0.5);
printf("regular: %.18e, accurate: %.18e, equal: %d\n", regular, accurate, regular == accurate);
return 0;
} I get this output:
And testing these values in Node.js too:
|
what do you think about to introduce 3rd lib to ensure mathematical correctness? IMO it can ensure compliance with ECMA specifications to the greatest extent possible. According to spec:
|
When exponent > 0, powi opt will not cause loss of precision at current nodejs version