5

In IEEE 754-2008 section "9.2.1 Special values" there is mentioned that

pow(+1, y) is 1 for any y (even a quiet NaN)

For not reading the entire document Wikipedia gives the shortcut:

The 2008 version of the IEEE 754 standard says that pow(1, qNaN) and pow(qNaN, 0) should both return 1 since they return 1 whatever else is used instead of quiet NaN.

Why then Math.pow(1, NaN) is NaN in JavaScript? Doesn't it follow the standards?

VisioN
  • 138,460
  • 30
  • 271
  • 271

3 Answers3

5

It's because the ECMAscript specification seems to say so.

pow (x, y)

Returns an implementation-dependent approximation to the result of raising x to the power y.

  • If y is NaN, the result is NaN.
  • ... other constraints...
Matt
  • 72,564
  • 26
  • 147
  • 178
Joachim Isaksson
  • 170,943
  • 22
  • 265
  • 283
  • Well, of course, this is the reason. So it looks like JavaScript doesn't follow the standards? – VisioN Feb 18 '13 at 13:55
  • 1
    No, JavaScript follows the standards. It's the fact that even though `NaN` holds a numerical value, it does not compute as a number. Therefore, when `Math.pow(1, NaN)` is computed, it is not computed with NaN as a number. – Achrome Feb 18 '13 at 13:59
  • 2
    @AshwinMukhija I didn't get your point. IEEE 754 explicitly states that `pow(1, NaN)` is *always* `1`, while ECMAscript spec states the opposite. – VisioN Feb 18 '13 at 14:01
  • 2
    Well, I wouldn't particularly expect the 2009 ECMAScript specification to be taking into account the 2008 changes to the IEEE-754 specification. Except that it does. Looking at the bibiliography, it makes reference specifically to IEEE Std 754-2008. So this does look like a spec miss. – Scott Sauyet Feb 18 '13 at 14:04
2

According to the Wikipedia article that pow definition was added to IEEE 754 in 2008. It's also optional.

On the other hand, ECMAScript's pow has been defined to return NaN if the second argument is NaN since at least 1997, in section 15.8.2.13 of that year's standard.

It would seem the ECMA committee chose to maintain compatibility with over a decade of JavaScript over complying with IEEE's peculiar new suggestion.

Samuel Edwin Ward
  • 6,386
  • 3
  • 31
  • 60
1

This is because NaN is "Not a Number", some kind of a mathematical undefined. In math 1 in the power of undefined is undefined as well.

qballer
  • 1,883
  • 2
  • 19
  • 39