范围错误:BigInt 除以零
当 BigInt
除以 0n
时,会发生 JavaScript 异常 "BigInt 除以零"。
¥The JavaScript exception "BigInt division by zero" occurs when a BigInt
is divided by 0n
.
信息
错误类型
什么地方出了错?
¥What went wrong?
division 或 remainder 运算符的除数是 0n
。在 Number
算术中,这会产生 Infinity
,但 BigInt 中没有 "无穷大值",因此会发出错误。在进行除法之前检查除数是否为 0n
。
¥The divisor of a division or remainder operator is 0n
. In Number
arithmetic, this produces Infinity
, but there's no "infinity value" in BigInts, so an error is issued. Check if the divisor is 0n
before doing the division.
示例
除以 0n
¥Division by 0n
js
const a = 1n;
const b = 0n;
const quotient = a / b;
// RangeError: BigInt division by zero
相反,首先检查除数是否为 0n
,然后发出带有更好消息的错误,或者回退到不同的值,例如 Infinity
或 undefined
。
¥Instead, check if the divisor is 0n
first, and either issue an error with a better message, or fallback to a different value, like Infinity
or undefined
.
js
const a = 1n;
const b = 0n;
const quotient = b === 0n ? undefined : a / b;