JavaScript: Calculate the nth root of a number

56,699

Solution 1

Can you use something like this?

Math.pow(n, 1/root);

eg.

Math.pow(25, 1/2) == 5

Solution 2

The nth root of x is the same as x to the power of 1/n. You can simply use Math.pow:

var original = 1000;
var fourthRoot = Math.pow(original, 1/4);
original == Math.pow(fourthRoot, 4); // (ignoring floating-point error)

Solution 3

Use Math.pow()

Note that it does not handle negative nicely - here is a discussion and some code that does

http://cwestblog.com/2011/05/06/cube-root-an-beyond/

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){}
}

Solution 4

You could use

Math.nthroot = function(x,n) {
    //if x is negative function returns NaN
    return this.exp((1/n)*this.log(x));
}
//call using Math.nthroot();

Solution 5

For the special cases of square and cubic root, it's best to use the native functions Math.sqrt and Math.cbrt respectively.

As of ES7, the exponentiation operator ** can be used to calculate the nth root as the 1/nth power of a non-negative base:

let root1 = Math.PI ** (1 / 3); // cube root of π

let root2 = 81 ** 0.25;         // 4th root of 81

This doesn't work with negative bases, though.

let root3 = (-32) ** 5;         // NaN
Share:
56,699
Nathan
Author by

Nathan

Updated on August 07, 2020

Comments

  • Nathan
    Nathan almost 4 years

    I'm trying to get the nth root of a number using JavaScript, but I don't see a way to do it using the built in Math object. Am I overlooking something?
    If not...

    Is there a math library I can use that has this functionality?
    If not...

    What's the best algorithm to do this myself?

  • Richard H
    Richard H over 12 years
    This will work if the pow function can take a fractional exponent. Not sure, but it should :)
  • mplungjan
    mplungjan over 12 years
    it does but does not handle negative numbers
  • Debosmit Ray
    Debosmit Ray about 8 years
    A small note. The pow function approximates the answer. So, for large values, this approximation can return very wrong numbers. [reference]. The same is true for the JS implementation. ref
  • Moritz
    Moritz over 7 years
    "nthRoot(-4, 2); // NaN (there is no solution)" well... at least not in real numbers
  • Qian Chen
    Qian Chen over 5 years
    How to handle Math.pow(-32, 1/5)?
  • Qian Chen
    Qian Chen over 5 years
    How about Math.pow(-32, 1/5)?
  • dotnetCarpenter
    dotnetCarpenter about 4 years
    After seeing stackoverflow.com/a/46268374/205696 I found a few optimizations to nthRoot. Since Math.pow(-4, 1/2) returns NaN and since we only need Math.abs for negative numbers, we can use Math.abs only for negative and odd numbers (not sure the latter is an optimization). So in one line: let nthRoot = (x, n) => n % 2 === 1 && x < 0 ? -(Math.abs(x) ** (1/n)) : x ** (1/n)
  • Mattia S.
    Mattia S. over 3 years
    Please use a switch statement
  • Gust van de Wal
    Gust van de Wal about 3 years
    @QianChen Always make the base be positive (-32 ➜ 32). Then, if the exponent is odd (5, so yes), turn the result negative (2 ➜ -2)
  • Mister SirCode
    Mister SirCode over 2 years
    with that many if statements, itll take longer to compute the function itself in high activity situations, so none of those checks will really matter at that point