INNER CODE UNIT · TypeScript
roots
margelo/react-native-graph · src/GetYForX.ts:55
roots = [cuberoot(-q)];
} else if (Math.abs(q) < 1e-8) {
// q = 0 -> t^3 + pt = 0 -> t(t^2+p)=0
roots = [0].concat(p < 0 ? [Math.sqrt(-p), -Math.sqrt(-p)] : []);
} else {
const D = (q * q) / 4 + (p * p * p) / 27;
if (Math.abs(D) < 1e-8) {
// D = 0 -> two roots
roots = [(-1.5 * q) / p, (3 * q) / p];
} else if (D > 0) {
// Only one real root
const u = cuberoot(-q / 2 - Math.sqrt(D));
roots = [u - p / (3 * u)];
} else {
// D < 0, three roots, but needs to use complex numbers/trigonometric solution
const u = 2 * Math.sqrt(-p / 3);
const t = Math.acos((3 * q) / p / u) / 3; // D < 0 implies p < 0 and acos argument in [-1..1]
const k = (2 * Math.PI) / 3;