Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating cubic root for negative number

So, to be short,

3√(-8) = (-8)1/3

console.log(Math.pow(-8,1/3));
//Should be -2

But when I test it out, it outputs

NaN

Why? Is it a bug or it is expected to be like this in the first place? I am using JavaScript to draw graphs, but this messes up the graph.

like image 243
Derek 朕會功夫 Avatar asked Oct 10 '12 01:10

Derek 朕會功夫


People also ask

Can you calculate the cube root of a negative number?

The cube root of a positive number is always positive, and the cube root of a negative number is always negative.


2 Answers

You can use this snippet to calculate it. It also works for other powers, e.g. 1/4, 1/5, etc.

function nthroot(x, n) {
  try {
    var negate = n % 2 == 1 && x < 0;
    if(negate)
      x = -x;
    var possible = Math.pow(x, 1 / n);
    n = Math.pow(possible, n);
    if(Math.abs(x - n) < 1 && (x > 0 == n > 0))
      return negate ? -possible : possible;
  } catch(e){}
}

nthroot(-8, 3);

Source: http://gotochriswest.com/blog/2011/05/06/cube-root-an-beyond/

A faster approach for just calculating the cubic root:

Math.cbrt = function(x) {
    var sign = x === 0 ? 0 : x > 0 ? 1 : -1;

    return sign * Math.pow(Math.abs(x), 1 / 3);
}

Math.cbrt(-8);

Update

To find an integer based cubic root, you can use the following function, inspired by this answer:

// positive-only cubic root approximation
function cbrt(n)
{
    var a = n; // note: this is a non optimized assumption

    while (a * a * a > n) {
        a = Math.floor((2 * a + (n / (a * a))) / 3);
    }

    return a;
}

It starts with an assumption that converges to the closest integer a for which a^3 <= n. This function can be adjusted in the same way to support a negative base.

like image 67
Ja͢ck Avatar answered Sep 21 '22 20:09

Ja͢ck


There's no bug; you are raising a negative number to a fractional power; hence, the NaN.

The top hit on google for this is from Dr Math the explanation is pretty good. It says for for real numbers (not complex numbers anyway), a negative number raised to a fractional power may not be a real number. The simplest example is probably

-4 ^ (1/2)

which is essentially computing the square root of -4. Even though the cubic root of -8 does have real solutions, I think that most software libraries find it more efficient not to do all the complex arithmetic and return NaN only when the imaginary part is nonzero and give you the nice real answer otherwise.

EDIT

Just to make absolutely clear that NaN is the intended result, see the official ECMAScript 5.1 Specification, Section 15.8.2.13. It says:

If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.

Again, even though SOME instances of raising negative numbers to fractional powers have exactly one real root, many languages just do the NaN thing for all cases of negative numbers to fractional roots.

Please do not think JavaScript is the only such language. C++ does the same thing:

If x is finite negative and y is finite but not an integer value, it causes a domain error.

like image 41
Ray Toal Avatar answered Sep 21 '22 20:09

Ray Toal