mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
`1 ** x` with x = Infinity printed 1 where Node prints NaN: the operator lowered to libm's pow, which answers 1 for pow(±1, ±Infinity) and pow(1, NaN) (and LLVM folds pow(1.0, x) to 1.0). Such operands now keep the direct pow call behind an inline guard that selects NaN when the base is ±1 and the exponent is not finite. A finite literal exponent (x ** 2) or a literal base other than ±1 (2 ** k) cannot meet those cases and keeps the bare call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
17 lines
663 B
TypeScript
17 lines
663 B
TypeScript
// `**` answers NaN for a base of ±1 with a NaN or infinite exponent, where
|
|
// libm's pow (and LLVM's folding of pow(1, x)) answers 1.
|
|
const values = [Infinity, -Infinity, NaN, 0, -0, 2, 0.5];
|
|
const read = (i: number): number => values[i % values.length]!;
|
|
for (let i = 0; i < values.length; i++) {
|
|
const x = read(i);
|
|
console.log(x, 1 ** x, (-1) ** x, x ** 0, x ** 1, 2 ** x, x ** -1);
|
|
}
|
|
let base = 1;
|
|
let exponent = Infinity;
|
|
console.log(base ** exponent, (-base) ** exponent, base ** NaN);
|
|
exponent = 3;
|
|
console.log(base ** exponent, (-1) ** exponent);
|
|
let acc = 1;
|
|
acc **= Infinity;
|
|
console.log(acc, Math.pow(1, Infinity), NaN ** 0, (-0) ** -1, 0 ** -1);
|