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
  • 143,310
  • 32
  • 282
  • 281
  • Probably it should be "pow(+1, y) is 1 for any y where y is a number", however NaN is not a number.. – Shadow The GPT Wizard Feb 18 '13 at 13:52
  • @ShadowWizard — [NaN is a number](http://stackoverflow.com/questions/2801601/why-does-typeof-nan-return-number) :) – Quentin Feb 18 '13 at 13:54
  • @Quentin - NaN is a number, but still not a number. :) – Achrome Feb 18 '13 at 13:55
  • Or you could just properly ensure that your parameters are numbers before passing them to `pow` and none of this would matter – jbabey Feb 18 '13 at 14:01
  • @FelixKling Yes, but this is a *special* case which is described separately. – VisioN Feb 18 '13 at 14:08
  • I noticed that, deleted my comment. I think you even missed the interesting part, because the whole line is actually: *"pow(+1, y) is 1 for any y (even a quiet NaN) "*. – Felix Kling Feb 18 '13 at 14:09
  • @FelixKling Yes, I skipped it for some reason. Added to the question with Wiki clarification as well. – VisioN Feb 18 '13 at 14:14

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
  • 74,352
  • 26
  • 153
  • 180
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 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,526
  • 3
  • 34
  • 62
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
  • 2,033
  • 2
  • 22
  • 40