created by Brian LeRoux & Andrew Lunny. sparodically uncurated by David Trejo.

2014 02 20 math pow

The spec of ECMAScript specifies some default values of Math.pow.

    console.log(Math.pow(NaN, 42) === NaN)

Oops, sorry.

    console.log(Math.pow(NaN, 42)); // NaN

As you can see, if the first argument is NaN, Math.pow returns NaN, which is quite normal. However, there's an exception: when the second argument is 0 (+0 or -0).

    console.log(Math.pow(NaN, 0)); // 1

When the second argument is 0, then Math.pow should return 1, no matter what the first argument is. This can be considered as a normal behavior, unless...

    console.log(Math.pow(Infinity, 0), Math.pow(0, 0)); // Still 1?

Basically the spec asserts values of some indeterminate forms. Sure, some can argue that lim(x->infinity) x^0 is zero (infinity is not a real number, anyway) and 0^0 is usually considered as one, but according to the same spec...

    console.log(Math.pow(1, Infinity)); // NaN

So 1^infinity is NaN while infinity^0 is 1. Great.


Fork me on GitHub