I’m writing a script that has to do something like this at one point: Math.pow(-2,1.5). The result should be approximately -2.82843, but instead, Javascript returns NaN. (I tried this in both Google Chrome 17 and Mozilla Firefox 11.) If the exponent is an integer, such as in Math.pow(-2,3), then Javascript will return the right answer, which, in this case, is -8. Positive numbers also correctly raise to non-integer powers; Math.pow(2,1.5) evaluates to approximately 2.8284271247461903. Is there any way that I can get Javascript to calculate the value of a negative number to a non-integer power?
I’m writing a script that has to do something like this at one point:
Share
Math.pow(-2, 1.5)returns NaN because there is no real number which equals -2 taken to the power 1.5. There is a complex number with this property, butMath.pow()doesn’t do calculations using complex numbers.This simple transformation demonstrates that this is the case:
(-2)1.5 = (-2)1 * (-2)0.5 = (-2) * sqrt(-2) = (-2) * i * sqrt(2) = -2i * sqrt(2)