I observed the following Javascript behavior:
> Math.pow(4, 2)
16
> Math.pow(4, 2.1)
18.37917367995256
> Math.pow(4, 0.5)
2
> Math.pow(-4, 2)
16
> Math.pow(-4, 2.1)
NaN
> Math.pow(-4, 0.5)
NaN
Why giving a negative number and a non-integer but rational number, makes Math.pow
to return NaN
?
For example, why Math.pow(4, 0.5)
is not NaN
but Math.pow(4, -0.5)
is NaN
?
Description. The pow( ) method can be used to raise any number to any power. If exponent is negative, pow( ) returns 1 / ( base abs(exponent)).
NaN number The number type in JavaScript is a set of all number values, including "Not A Number", positive infinity and negative infinity.
The signature of the pow() method is as follows: The method takes the base and exponent parameters of type double . The method calculates multiplication of the base with itself exponent times and returns the result of type double .
It's a static method on Math class, which means you don't have to instantiate a Math instance to call it. The power of a number is the number of times the number is multiplied by itself.
Imaginary (Complex) Number Alert!
I have got a crazy result too. So what I have here is the same logic in different programming languages.
What I feel is, when the power is calculated, the resulting number is root of -1, which is an imaginary number. These programs cannot handle imaginary numbers is my guess.
The results are as follows:
C#
Math.Pow(-1.1, -1.1); // Returns NaN
Java Output
Math.pow(-4, 2.1); // Returns NaN
JavaScript
Math.pow(-4, 2.1); // Returns NaN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With