Estoy tratando de obtener la raíz enésima de un número usando JavaScript, pero no veo una forma de hacerlo usando el Math objeto . ¿Estoy pasando por alto algo?
Si no...
¿Existe una biblioteca matemática que pueda usar que tenga esta funcionalidad?
Si no...
¿Cuál es el mejor algoritmo para hacer esto yo mismo?
algorithm
math
javascript
Nathan
fuente
fuente

Respuestas:
¿Puedes usar algo como esto?
Math.pow(n, 1/root);p.ej.
Math.pow(25, 1/2) == 5fuente
Math.pow(-32, 1/5)?La
nraíz de thxes la mismaxque la potencia de1/n. Simplemente puede usarMath.pow:var original = 1000; var fourthRoot = Math.pow(original, 1/4); original == Math.pow(fourthRoot, 4); // (ignoring floating-point error)fuente
Utilice Math.pow ()
Tenga en cuenta que no maneja bien los negativos: aquí hay una discusión y un código que sí
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){} }fuente
Podrías usar
Math.nthroot = function(x,n) { //if x is negative function returns NaN return this.exp((1/n)*this.log(x)); } //call using Math.nthroot();fuente
La
nraíz -ésima dexes un númerortal que elevadora la potencia de1/nesx.En números reales, hay algunas subcasas:
xes positivo yres par.xes positiva yres impar.xes negativa yres impar.xes negativo yres par.Dado que
Math.powno le gusta una base negativa con un exponente no entero, puede usarfunction 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); }Ejemplos:
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)fuente
nthRoot. Dado queMath.pow(-4, 1/2)devuelveNaNy dado que solo necesitamosMath.absnúmeros negativos, podemos usarMath.abssolo números negativos e impares (no estoy seguro de que este último sea una optimización). Entonces en una línea:let nthRoot = (x, n) => n % 2 === 1 && x < 0 ? -(Math.abs(x) ** (1/n)) : x ** (1/n)Para los casos especiales de raíz cuadrada y cúbica, es mejor usar las funciones nativas
Math.sqrtyMath.cbrtrespectivamente.A partir de ES7, el operador de exponenciación
**se puede utilizar para calcular la raíz n -ésima como la 1 / n- ésima potencia de una base no negativa:let root1 = Math.PI ** (1 / 3); // cube root of π let root2 = 81 ** 0.25; // 4th root of 81Sin embargo, esto no funciona con bases negativas.
let root3 = (-32) ** 5; // NaNfuente
Aquí hay una función que intenta devolver el número imaginario. También comprueba primero algunas cosas comunes, por ejemplo: si obtiene la raíz cuadrada de 0 o 1, o si obtiene la raíz 0 del número x
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; }fuente
Bueno, sé que esta es una vieja pregunta. Pero, según la respuesta de SwiftNinjaPro, simplifiqué la función y solucioné algunos problemas de NaN. Nota: Esta función utilizó la característica ES6, la función de flecha y cadenas de plantillas, y exponenciación. Por lo tanto, es posible que no funcione en navegadores más antiguos:
Math.numberRoot = (x, n) => { return (((x > 1 || x < -1) && n == 0) ? Infinity : ((x > 0 || x < 0) && n == 0) ? 1 : (x < 0 && n % 2 == 0) ? `${((x < 0 ? -x : x) ** (1 / n))}${"i"}` : (n == 3 && x < 0) ? -Math.cbrt(-x) : (x < 0) ? -((x < 0 ? -x : x) ** (1 / n)) : (n == 3 && x > 0 ? Math.cbrt(x) : (x < 0 ? -x : x) ** (1 / n))); };Ejemplo:
Math.numberRoot(-64, 3); // Returns -4Ejemplo (resultado numérico imaginario):
Math.numberRoot(-729, 6); // Returns a string containing "3i".fuente
Escribí un algoritmo pero es lento cuando necesitas muchos números después del punto:
https://github.com/am-trouzine/Arithmetic-algorithms-in-different-numeral-systems
La función devuelve una cadena.
P.ej
var original = 1000; var fourthRoot = NRoot(original, 4, 10, 32); console.log(fourthRoot); //5.62341325190349080394951039776481fuente