Skip to content

Improve quadratic and quartic Precision and Accuracy, add more tests#37

Merged
vorot merged 1 commit into
vorot:masterfrom
Safari77:quadfixes
Feb 9, 2026
Merged

Improve quadratic and quartic Precision and Accuracy, add more tests#37
vorot merged 1 commit into
vorot:masterfrom
Safari77:quadfixes

Conversation

@Safari77

@Safari77 Safari77 commented Feb 8, 2026

Copy link
Copy Markdown
Contributor
  1. Precision & Accuracy Discriminant Calculation: Replaced the naive bb - 4a*c with Kahan Summation and Veltkamp-Dekker Splitting. This effectively emulates higher precision (software FMA) to recover lost bits when b2≈4ac, fixing catastrophic cancellation errors.

Root Calculation: Switched to the Citardauq Strategy. The solver now calculates the larger root q first (using the sign of b) to avoid subtractive cancellation, then computes the second root as c/q.

  1. Numerical Stability (Overflow/Underflow) Global Scaling: Implemented coefficient normalization. Inputs are scaled by 1/max(∣a∣,∣b∣,∣c∣) before calculation. This allows the solver to handle extreme values (e.g., 10300 or 10−300) without intermediate overflow or underflow, which the original version failed on.

Discriminant Tolerance: Added a check to treat tiny negative discriminants (caused by floating-point noise) as zero.

  1. Robustness & Edge Cases Degenerate Cases: Added explicit handling for a=0 (linear fallback), c=0 (roots 0,−b/a), and b=0 (pure quadratic).

Safety: Added rigorous checks for NaN and Infinity inputs, returning Roots::No instead of undefined behavior.

  1. Testing Comprehensive Suite: Ported the tests.jl test suite from https://github.com/goualard-f/QuadraticEquation.jl For quartic.rs, added more tests also.

1. Precision & Accuracy
Discriminant Calculation: Replaced the naive b*b - 4*a*c with Kahan
Summation and Veltkamp-Dekker Splitting. This effectively emulates
higher precision (software FMA) to recover lost bits when b2≈4ac, fixing
catastrophic cancellation errors.

Root Calculation: Switched to the Citardauq Strategy. The solver now
calculates the larger root q first (using the sign of b) to avoid
subtractive cancellation, then computes the second root as c/q.

2. Numerical Stability (Overflow/Underflow)
Global Scaling: Implemented coefficient normalization. Inputs are scaled
by 1/max(∣a∣,∣b∣,∣c∣) before calculation. This allows the solver to
handle extreme values (e.g., 10300 or 10−300) without intermediate
overflow or underflow, which the original version failed on.

Discriminant Tolerance: Added a check to treat tiny negative discriminants (caused by floating-point noise) as zero.

3. Robustness & Edge Cases
Degenerate Cases: Added explicit handling for a=0 (linear fallback), c=0
(roots 0,−b/a), and b=0 (pure quadratic).

Safety: Added rigorous checks for NaN and Infinity inputs, returning
Roots::No instead of undefined behavior.

4. Testing
Comprehensive Suite: Ported the tests.jl test suite from https://github.com/goualard-f/QuadraticEquation.jl
For quartic.rs, added more tests also.
@vorot vorot merged commit e3ac305 into vorot:master Feb 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants