Improve quadratic and quartic Precision and Accuracy, add more tests#37
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Discriminant Tolerance: Added a check to treat tiny negative discriminants (caused by floating-point noise) as zero.
Safety: Added rigorous checks for NaN and Infinity inputs, returning Roots::No instead of undefined behavior.