As already said the hugomg, is a second-degree equation, and they all have the following form:
Ax2 + bx + c = 0
In your case:
a = 1056261433
b = 431977909
c = -281860832 - 2.022 = -281860834.022
These equations can be solved by Bhaskara’s formula:
Image of the formula: Wikipedia
In Javascript, a simple implementation takes the values of a
, b
and c
and returns the two possible results:
var a = 1056261433;
var b = 431977909;
var c = -281860834.022;
function bhaskara(a, b, c) {
var ret = [];
var d = delta(a, b, c);
ret[0] = ((b * -1) - Math.sqrt(d)) / (2 * a);
ret[1] = ((b * -1) + Math.sqrt(d)) / (2 * a);
return ret;
// calcula o delta separadamente
function delta(a, b, c) {
return Math.pow(b, 2) - (4 * a * c);
}
}
document.body.innerHTML = bhaskara(a, b, c).join(', ');
The limitation of this code is that it does not deal with complex numbers, if the delta of the equation is negative. If you need to deal with this type of value, you would need to create a representation of complexes like arrays or objects, or use a library like the Math.js.
This is a question about how to solve mathematical operation and not exactly a language resource. Javascript can like virtually any other language determine the "X" (variable) of an arithmetic calculus, if you create (or use an existing) function for it.
– Gabriel Gartz
Related: "Equation solving"
– mgibsonbr