Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Math.pow(a, b) is NaN when a is a negative number and b is a non-integer? [duplicate]

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?

like image 601
Ionică Bizău Avatar asked Nov 27 '13 18:11

Ionică Bizău


People also ask

Does Math POW work with negative numbers?

Description. The pow( ) method can be used to raise any number to any power. If exponent is negative, pow( ) returns 1 / ( base abs(exponent)).

Is NaN a negative number?

NaN number The number type in JavaScript is a set of all number values, including "Not A Number", positive infinity and negative infinity.

Does Math POW require a double?

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 .

Why is Math POW a static method?

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.


1 Answers

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
like image 151
Praveen Kumar Purushothaman Avatar answered Oct 05 '22 20:10

Praveen Kumar Purushothaman