The JavaScript exception "BigInt negative exponent" occurs when a BigInt
is raised to the power of a negative BigInt value.
The JavaScript exception "BigInt negative exponent" occurs when a BigInt
is raised to the power of a negative BigInt value.
The exponent of an exponentiation operation must be positive. Since negative exponents would take the reciprocal of the base, the result will be between -1 and 1 in almost all cases, which gets rounded to 0n
. To catch mistakes, negative exponents are not allowed. Check if the exponent is non-negative before doing exponentiation.
Instead, check if the exponent is negative first, and either issue an error with a better message, or fallback to a different value, like 0n
or undefined
.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/BigInt_negative_exponent