Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Math.pow(1, NaN) equals NaN in JavaScript?

Tags:

javascript

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?

like image 298
VisioN Avatar asked Feb 18 '13 13:02

VisioN


Video Answer


2 Answers

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...
like image 166
Joachim Isaksson Avatar answered Sep 17 '22 11:09

Joachim Isaksson


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.

like image 28
Samuel Edwin Ward Avatar answered Sep 19 '22 11:09

Samuel Edwin Ward