JavaScript를 사용하여 숫자의 n 번째 루트를 얻으려고하는데 내장 Math객체를 사용하여 수행하는 방법이 보이지 않습니다 . 내가 뭔가를 간과하고 있습니까? 
그렇지 않다면 …
이 기능이있는 수학 라이브러리를 사용할 수 있습니까? 
그렇지 않다면 …
이 작업을 직접 수행하는 가장 좋은 알고리즘은 무엇입니까?
답변
이와 같은 것을 사용할 수 있습니까?
Math.pow(n, 1/root);
예.
Math.pow(25, 1/2) == 5
답변
의 nth 루트는 의 거듭 제곱과 x동일 합니다. 다음을 간단히 사용할 수 있습니다 .x1/nMath.pow
var original = 1000;
var fourthRoot = Math.pow(original, 1/4);
original == Math.pow(fourthRoot, 4); // (ignoring floating-point error)
답변
Math.pow () 사용
부정적인 것을 잘 처리하지 않는다는 점에 유의하십시오. 여기에 토론과 일부 코드가 있습니다.
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){}
}
답변
당신은 사용할 수 있습니다
Math.nthroot = function(x,n) {
    //if x is negative function returns NaN
    return this.exp((1/n)*this.log(x));
}
//call using Math.nthroot();
답변
n의 번째 루트는 x숫자입니다 r있도록 r전원의가 1/n있다 x.
실수에는 몇 가지 하위 사례가 있습니다.
- x양수이고- r짝수 일 때 두 가지 솔루션 (반대 부호가있는 동일한 값) 이 있습니다.
- x긍정적이고- r홀수 일 때 하나의 긍정적 인 해결책이 있습니다 .
- x음수이고- r홀수 일 때 하나의 음수 솔루션이 있습니다 .
- x음수이고 균등할 때는 해결책이 없습니다- r.
Math.pow정수가 아닌 지수를 가진 음수를 좋아하지 않기 때문에 다음을 사용할 수 있습니다.
function nthRoot(x, n) {
  if(x < 0 && n%2 != 1) return NaN; // Not well defined
  return (x < 0 ? -1 : 1) * Math.pow(Math.abs(x), 1/n);
}
예 :
nthRoot(+4, 2); // 2 (the positive is chosen, but -2 is a solution too)
nthRoot(+8, 3); // 2 (this is the only solution)
nthRoot(-8, 3); // -2 (this is the only solution)
nthRoot(-4, 2); // NaN (there is no solution)
답변
답변
허수를 반환하는 함수가 있습니다. 또한 몇 가지 일반적인 사항을 먼저 확인합니다. 예 : 0 또는 1의 제곱근을 얻거나 숫자 x의 0 번째 루트를 얻는 경우
function root(x, n){
        if(x == 1){
          return 1;
        }else if(x == 0 && n > 0){
          return 0;
        }else if(x == 0 && n < 0){
          return Infinity;
        }else if(n == 1){
          return x;
        }else if(n == 0 && x > 1){
          return Infinity;
        }else if(n == 0 && x == 1){
          return 1;
        }else if(n == 0 && x < 1 && x > -1){
          return 0;
        }else if(n == 0){
          return NaN;
        }
        var result = false;
        var num = x;
        var neg = false;
        if(num < 0){
            //not using Math.abs because I need the function to remember if the number was positive or negative
            num = num*-1;
            neg = true;
        }
        if(n == 2){
            //better to use square root if we can
            result = Math.sqrt(num);
        }else if(n == 3){
            //better to use cube root if we can
            result = Math.cbrt(num);
        }else if(n > 3){
            //the method Digital Plane suggested
            result = Math.pow(num, 1/n);
        }else if(n < 0){
            //the method Digital Plane suggested
            result = Math.pow(num, 1/n);
        }
        if(neg && n == 2){
            //if square root, you can just add the imaginary number "i=√-1" to a string answer
            //you should check if the functions return value contains i, before continuing any calculations
            result += 'i';
        }else if(neg && n % 2 !== 0 && n > 0){
            //if the nth root is an odd number, you don't get an imaginary number
            //neg*neg=pos, but neg*neg*neg=neg
            //so you can simply make an odd nth root of a negative number, a negative number
            result = result*-1;
        }else if(neg){
            //if the nth root is an even number that is not 2, things get more complex
            //if someone wants to calculate this further, they can
            //i'm just going to stop at *n√-1 (times the nth root of -1)
            //you should also check if the functions return value contains * or √, before continuing any calculations
            result += '*'+n+√+'-1';
        }
        return result;
    }
