numworks / epsilon

Modern graphing calculator operating system.
https://www.numworks.com/resources/engineering/software/
1.74k stars 460 forks source link

Discriminant for quadratic equation incorrectly computed #2038

Open goualard-f opened 1 year ago

goualard-f commented 1 year ago

Describe the bug

It seems that the computation of the discriminant for quadratic equations uses the textbook formula $\Delta=b^2-4ac$ (source), which is flawed for some inputs. As a consequence, some equations are not solved correctly. Case in point:

$x^2 + (1+2^{-52})x + (0.25+2^{-53}) = 0$

All parameters are representable with double precision floating-point numbers. The solutions are also perfectly representable: $x_1=-\frac{1}{2}$ and $x_2=-\frac{1+2^{-51}}{2}$.

However, Numworks gets both the number of solutions and the solution wrong as it claims that there is only one solution equal to $\frac{4503599627370497}{9007199254740992}=-\frac{1+2^{-52}}{2}$.

Screenshots

screenshot(1)

screenshot

To Reproduce

Steps to reproduce the behavior:

  1. Go to the 'Equations' app
  2. Type the equation above
  3. Solve

Expected behavior

The quadratic solver should use a better algorithm to compute the discriminant so as to avoid returning the wrong number of solutions whenever possible. The state of the art is nicely summarized, albeit without references, in the blog post by Panchekha about the implementation of quadratic-solutions in the racket programming language.

Environment

artaxxx commented 1 year ago

Thank you @goualard-f !

goualard-f commented 1 year ago

Also, this is not a matter of discriminant computation only. Try $x^2+2^{27}x+\frac{3}{4}=0$. It should have the solutions -134217728 and -5.587935447692871e-9. There is a catastrophic cancellation in computing the second solution with the naive formula, and the Numworks calculator computes the value $\approx$-7.45e-9. The algorithm should use the modified Fagnano's formulas to compute the solutions.