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

后端 未结 9 695
伪装坚强ぢ
伪装坚强ぢ 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.

    0 讨论(0)
  • 2020-11-30 02:00

    It is just convention to define it as 1, 0 or to leave it undefined. The definition pow(0,0) is wide spread because of the following definition:

    mathematical power definition


    ECMA-Script documentation says the following about pow(x,y):

    • 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.

    [ http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.13 ]

    0 讨论(0)
  • 2020-11-30 02:10

    When you want to know what value you should give to f(a) when f isn't directly computable in a, you compute the limit of f when x tends towards a.

    In case of x^y, usual limits tend towards 1 when x and y tend to 0, and especially x^x tends towards 1 when x tends to 0.

    See http://www.math.hmc.edu/funfacts/ffiles/10005.3-5.shtml

    0 讨论(0)
  • 2020-11-30 02:11

    For this to understand you need to solve calculus:

    Expanding x^x around zero using Taylor series, we get:

    So to understand what's going on with limit when x goes to zero, we need to find out what's going on with second term x log(x), because other terms are proportional to x log(x) raised to some power.

    We need to use transformation:

    Now after this transformation we can use L'Hôpital's rule, which states that:

    So differentiating that transformation we get:

    So we've calculated that term log(x)*x approaches 0 when x approaches 0. It's easy to see that other consecutive terms also approaches zero and even faster than second term.

    So at point x=0, series becomes 1 + 0 + 0 + 0 + ... and thus equals to 1.

    0 讨论(0)
  • 2020-11-30 02:12

    Donald Knuth

    sort of settled this debate in 1992 with the following:

    enter image description here

    And went even more into details in his paper Two Notes on Notation.

    Basically, while we don't have 1 as the limit of f(x)/g(x) for all not all functions f(x) and g(x), it still makes combinatorics so much simpler to define 0^0=1, and then just make special cases in the few places where you need to consider functions such as 0^x, which are weird anyway. After all x^0 comes up a lot more often.

    Some of the best discussions I know of this topic (other than the Knuth paper) are:

    • http://mathforum.org/dr.math/faq/faq.0.to.0.power.html
    • http://www.quora.com/Mathematics/What-is-math-0-0-math?share=1
    • https://math.stackexchange.com/questions/475337/the-binomial-formula-and-the-value-of-00
    0 讨论(0)
  • 2020-11-30 02:12

    I'd like to disagree with some of the previous answers' assertion that it's a matter of convention or convenience (covering some special cases for various theorems, etc) that 0^0 be defined as 1 instead of 0.

    Exponentiation doesn't actually fit that well with our other mathematical notations, so the definition we all learn leaves room for confusion. A slightly different way of approaching it is to say that a^b (or exp(a, b), if you like) returns the value multiplicatively equivalent to multiplying some other thing by a, repeated b times.

    When we multiply 5 by 4, 2 times, we get 80. We've multiplied 5 by 16. So 4^2 = 16.

    When you multiply 14 by 0, 0 times, we are left with 14. We've multiplied it 1. Hence, 0^0 = 1.

    This line of thinking might also help to clarify negative and fractional exponents. 4^(-2) is a 16th, because 'negative multiplication' is division - we divide by four twice.

    a^(1/2) is root(a), because multiplying something by the root of a is half the multiplicative work as multiplying it by a itself - you would have to do it twice to multiply something by 4 = 4^1 = (4^(1/2))^2

    0 讨论(0)
提交回复
热议问题