Skip to content

fix: Math.pow constant optimization behaves inconsistently in different versions of node #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

Merged
merged 4 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/node": "^18.19.75",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"as-float": "^1.0.1",
"diff": "^7.0.0",
"esbuild": "^0.25.0",
"eslint": "^8.57.1",
Expand Down
1 change: 1 addition & 0 deletions src/glue/js/float.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ declare function f32_as_i32(value: f32): i32;
declare function i32_as_f32(value: i32): f32;
declare function f64_as_i64(value: f64): i64;
declare function i64_as_f64(value: i64): f64;
declare function f64_pow(value: f64, exponent: f64): f64;
4 changes: 4 additions & 0 deletions src/glue/js/float.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @license Apache-2.0
*/

import { f64_pow } from "as-float";

/* eslint-disable no-undef */

const F64 = new Float64Array(1);
Expand All @@ -29,3 +31,5 @@ globalThis.i64_as_f64 = function i64_as_f64(value) {
I32[1] = i64_high(value);
return F64[0];
};

globalThis.f64_pow = f64_pow;
14 changes: 4 additions & 10 deletions src/util/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ export function isPowerOf2(x: i32): bool {
export function accuratePow64(x: f64, y: f64): f64 {
if (!ASC_TARGET) { // ASC_TARGET == JS
// Engines like V8, WebKit and SpiderMonkey uses powi fast path if exponent is integer
// This speculative optimization leads to loose precisions like 10 ** 208 != 1e208
// or/and 10 ** -5 != 1e-5 anymore. For avoid this behaviour we are forcing exponent
// to fractional form and compensate this afterwards.
if (isFinite(y) && Math.abs(y) >= 2 && Math.trunc(y) == y) {
if (y < 0) {
return Math.pow(x, y + 0.5) / Math.pow(x, 0.5);
} else {
return Math.pow(x, y - 0.5) * Math.pow(x, 0.5);
}
}
// This speculative optimization leads to loose precisions like 10 ** -5 != 1e-5 anymore
// and introduces inconsistencies between different engines and versions
// For avoid this behavior we using bootstrap f64_pow function.
return f64_pow(x, y);
}
return Math.pow(x, y);
}