Method and source check
Last checked:
Automated source and implementation review, including worked examples and documented numerical limits. This review does not provide professional advice or specialist approval.
Method
For ax²+bx+c=0 with a nonzero, compute Δ=b²−4ac. Treat |Δ|≤2×Number.EPSILON×max(|b²|,|4ac|) as zero to handle roundoff; near this threshold classification is approximate. Use a stable q-based quadratic formula for distinct real roots and overflow-safe division for repeated and complex roots. Reject unsupported derived overflow or nonzero products that underflow to zero.
Δ = b² − 4ac
x = (−b ± √Δ)/(2a)
Variables and units
- a
- nonzero quadratic coefficient
- b
- linear coefficient
- c
- constant coefficient
- Δ
- discriminant
Worked example
For x²−3x+2=0, Δ=1 and the roots are 2 and 1; for x²+1=0, Δ=−4 and roots are ±i.
Limitations
Root components use six significant digits for display; this is not a guarantee of accuracy. Coefficients and the computed discriminant retain their numerical precision.
- Computed irrational roots are approximate, not exact symbolic expressions.
- The input must describe a quadratic equation (a ≠ 0).
- A near-zero discriminant within the relative floating-point error threshold is reported as a repeated root, not as an exact proof of root count. Roots are numerical approximations.
- Δ>0: two distinct real roots.
- Δ=0: one repeated real root.
- Δ<0: two complex conjugate roots.
- a=0.1,b=0.6,c=0.9 is treated as Δ=0 with repeated root −3.
- a=1,b=0,c=−1e-8 retains Δ=4e-8 and roots ±0.0001.
- a=1,b=±1e8,c=1 preserves the small root ∓1e-8 using stable quadratic arithmetic.
- a=1e308,b=1,c=1e-308 retains nonzero complex root components −5e-309 and ±8.66025e-309.