Estoy usando nerdamer.solve para resolver raíces, pero las raíces son largas y no están truncadas. Quería obtener valores truncados hasta 4 decimales. ¿Cómo puedo conseguir esto? Estoy usando el siguiente código para resolver y mostrar la salida en html:
var r = nerdamer.solve(`1 - ${a} * x^(${p}) + ${b}`, 'x'); document.getElementById("polesAns").innerHTML= r.toString();Se emite lo siguiente:
[(138655807/135201312)*i+49385501/48155102,(-138655807/135201312)*i+49385501/48155102,(58886197/57419096)*i-49385501/48155102,(-58886197/57419096)*i-49385501/48155102,-560373381/386371730,172668482/119053157,(145619303/100403024)*i-5753750945848186/10000000000000000000000000000000,(-560373381/386371730)*i-5753750945848186/10000000000000000000000000000000]No hay división realizada también.
Probé la solución publicada aquí: ¿Cómo usar .toFixed() (o cualquier alternativa) en las soluciones Nerdamer.solve?
Pero, ¿cómo puedo hacer esto con mi código? Intenté lo siguiente:
var value = `1 - ${a} * x^(${p}) + ${b}`; var toFixed = function(value, n) { var img = Number(nerdamer.imagpart(value).text()).toFixed(n); var real = Number(nerdamer.realpart(value).text()).toFixed(n); // Format the number assuming i denotes imaginary in your case var formatted = ''; if(real !== '0.0000') { formatted += real; } if(img !== '0.0000') { // Put the plus sign betweent the real and imaginary if(img.charAt(0) !== '-' && formatted) { formatted += '+'; } // Assuming you're using i and not j for instance formatted += img+'i'; } return formatted; }; sol_raw = this.nerdamer.solve(value,'s'); xs = this.nerdamer(sol_raw.toString()).each(function(solution) { roundedSolutions.push(toFixed(solution, 4)); }); this.setState({ solution: roundedSolution.join(''), equation:value}) document.getElementById("polesAns").value = solution.toString();No entiendo la parte this.setState(), ¿debería declarar sol_raw y xs como var?
También se usa la sustitución de variable en mi ecuación raíz anterior del consejo aquí javascript Resolver ecuación con sustitución de valor variable
gracias