Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Math.pow(-0, -7) === -Infinity?

Tags:

javascript

pow

Is there a rationale for Math.pow(-0, x) evaluating to Infinity for all negative x, except for the odd ones when it's -Infinity? I mean:

Math.pow(-0, -6);          // Infinity
Math.pow(-0, -7);          // -Infinity
Math.pow(-0, -7.33);       // Infinity
Math.pow(-0, -Infinity);   // Infinity

Fiddle

I'm aware that a positive odd power of a negative number is negative, but this is clearly not the case here: -7 is not a positive exponent, and while -0 is indeed a number if you open your mind, it's not a negative one.

This behaviour makes mathematically no sense, and I don't see a practical use or technical convenience for it either, so I suspect the specification must rely on historical/compatibility reasons. Any insights?

like image 699
GOTO 0 Avatar asked Apr 03 '14 07:04

GOTO 0


People also ask

Why does math POW return a double?

pow Example. The method raises a to the power of b and returns the result as double. In other words, a is multiplied by itself b times.

Does math POW work with negative numbers?

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

Can pow function takes 3 arguments?

The pow() function is used to find the nth power of a value. There are two types of pow() function: inbuilt and imported from the math module. The inbuilt pow() function accepts three arguments (base, exponent, and modulus), out of which the third argument (modulus) is optional.

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

I believe this is for consistency with powers of other negative numbers. When you raise a negative number to an even power, you always get a positive result. But when you raise a negative number to an odd power, you get a negative result. Raising zero to a negative power always results in infinity (because it's equivalent to dividing by zero); in the case of negative zero, the sign alternates just like other negative numbers.

like image 195
Barmar Avatar answered Sep 28 '22 12:09

Barmar