As already hugomg , this is a second-degree equation, and they all have the following form :
ax 2 + bx + c = 0
In your case:
a = 1056261433
b = 431977909
c = -281860832 - 2.022 = -281860834.022
These equations can be solved by the Bhaskara formula:
#
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 handle 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 the complexes as arrays or objects, or use a library like math.js .