The ECMAScript specification for Math.powhas the following peculiar rule:
- If x <0 and x is finite and y is finite and y is not an integer, the result is NaN.
( http://es5.github.com/#x15.8.2.13 )
The result Math.pow(-8, 1 / 3)is NaN, not-2
What is the reason for this rule? Is there some broader computer science or IEEEish reason for this rule, or is it just a TC39 / Eich choice made one at a time?
Update
Thanks to the exchange of Amadan with me, I think I now understand the reasoning. I would like to expand our discussion for the sake of posterity.
Take the following example: Math.pow(823543, 1 / 7)gives 6.999999999999999, although it really should be 7. This is an inaccuracy due to the fact that 1 / 7you must first convert to a decimal representation 0.14285714285714285, which is truncated and loses accuracy. This is not such a bad problem when we work with positive numbers, because we still get a result that is very close to the real result.
, , . JavaScript- Math.pow(-823543, 1 / 7), 1 / 7 , Math.pow(-823543, 0.14285714285714285), . , , NaN, , -7. , , , " ", , JS- .
, , , NaN - - , , , , .
, .