Why is Math.pow(0, 0) === 1?

后端 未结 9 736
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 01:47

We all know that 00 is indeterminate.

But, javascript says that:

Math.pow(0, 0) === 1 // true

9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 01:58

    In JavaScript Math.pow is defined as follows:

    • If y is NaN, the result is NaN.
    • If y is +0, the result is 1, even if x is NaN.
    • If y is −0, the result is 1, even if x is NaN.
    • If x is NaN and y is nonzero, the result is NaN.
    • If abs(x)>1 and y is +∞, the result is +∞.
    • If abs(x)>1 and y is −∞, the result is +0.
    • If abs(x)==1 and y is +∞, the result is NaN.
    • If abs(x)==1 and y is −∞, the result is NaN.
    • If abs(x)<1 and y is +∞, the result is +0.
    • If abs(x)<1 and y is −∞, the result is +∞.
    • If x is +∞ and y>0, the result is +∞.
    • If x is +∞ and y<0, the result is +0.
    • If x is −∞ and y>0 and y is an odd integer, the result is −∞.
    • If x is −∞ and y>0 and y is not an odd integer, the result is +∞.
    • If x is −∞ and y<0 and y is an odd integer, the result is −0.
    • If x is −∞ and y<0 and y is not an odd integer, the result is +0.
    • If x is +0 and y>0, the result is +0.
    • If x is +0 and y<0, the result is +∞.
    • If x is −0 and y>0 and y is an odd integer, the result is −0.
    • If x is −0 and y>0 and y is not an odd integer, the result is +0.
    • If x is −0 and y<0 and y is an odd integer, the result is −∞.
    • If x is −0 and y<0 and y is not an odd integer, the result is +∞.
    • If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.

    emphasis mine

    as a general rule, native functions to any language should work as described in the language specification. Sometimes this includes explicitly "undefined behavior" where it's up to the implementer to determine what the result should be, however this is not a case of undefined behavior.

提交回复
热议问题