Average Error: 33.7 → 10.9
Time: 12.2s
Precision: 64
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \le -1.98276540088900058 \cdot 10^{134}:\\ \;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)\\ \mathbf{elif}\;b \le 1.1860189201379418 \cdot 10^{-161}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2} \cdot \frac{1}{a}\\ \mathbf{else}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \end{array}\]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \le -1.98276540088900058 \cdot 10^{134}:\\
\;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)\\

\mathbf{elif}\;b \le 1.1860189201379418 \cdot 10^{-161}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2} \cdot \frac{1}{a}\\

\mathbf{else}:\\
\;\;\;\;-1 \cdot \frac{c}{b}\\

\end{array}
double f(double a, double b, double c) {
        double r99367 = b;
        double r99368 = -r99367;
        double r99369 = r99367 * r99367;
        double r99370 = 4.0;
        double r99371 = a;
        double r99372 = r99370 * r99371;
        double r99373 = c;
        double r99374 = r99372 * r99373;
        double r99375 = r99369 - r99374;
        double r99376 = sqrt(r99375);
        double r99377 = r99368 + r99376;
        double r99378 = 2.0;
        double r99379 = r99378 * r99371;
        double r99380 = r99377 / r99379;
        return r99380;
}

double f(double a, double b, double c) {
        double r99381 = b;
        double r99382 = -1.9827654008890006e+134;
        bool r99383 = r99381 <= r99382;
        double r99384 = 1.0;
        double r99385 = c;
        double r99386 = r99385 / r99381;
        double r99387 = a;
        double r99388 = r99381 / r99387;
        double r99389 = r99386 - r99388;
        double r99390 = r99384 * r99389;
        double r99391 = 1.1860189201379418e-161;
        bool r99392 = r99381 <= r99391;
        double r99393 = r99381 * r99381;
        double r99394 = 4.0;
        double r99395 = r99394 * r99387;
        double r99396 = r99395 * r99385;
        double r99397 = r99393 - r99396;
        double r99398 = sqrt(r99397);
        double r99399 = r99398 - r99381;
        double r99400 = 2.0;
        double r99401 = r99399 / r99400;
        double r99402 = 1.0;
        double r99403 = r99402 / r99387;
        double r99404 = r99401 * r99403;
        double r99405 = -1.0;
        double r99406 = r99405 * r99386;
        double r99407 = r99392 ? r99404 : r99406;
        double r99408 = r99383 ? r99390 : r99407;
        return r99408;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original33.7
Target21.0
Herbie10.9
\[\begin{array}{l} \mathbf{if}\;b \lt 0.0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if b < -1.9827654008890006e+134

    1. Initial program 56.8

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified56.8

      \[\leadsto \color{blue}{\frac{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2}}{a}}\]
    3. Taylor expanded around -inf 3.1

      \[\leadsto \color{blue}{1 \cdot \frac{c}{b} - 1 \cdot \frac{b}{a}}\]
    4. Simplified3.1

      \[\leadsto \color{blue}{1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)}\]

    if -1.9827654008890006e+134 < b < 1.1860189201379418e-161

    1. Initial program 10.3

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified10.3

      \[\leadsto \color{blue}{\frac{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2}}{a}}\]
    3. Using strategy rm
    4. Applied div-inv10.5

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2} \cdot \frac{1}{a}}\]

    if 1.1860189201379418e-161 < b

    1. Initial program 49.7

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified49.7

      \[\leadsto \color{blue}{\frac{\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2}}{a}}\]
    3. Taylor expanded around inf 13.7

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification10.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -1.98276540088900058 \cdot 10^{134}:\\ \;\;\;\;1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)\\ \mathbf{elif}\;b \le 1.1860189201379418 \cdot 10^{-161}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{2} \cdot \frac{1}{a}\\ \mathbf{else}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \end{array}\]

Reproduce

herbie shell --seed 2020047 +o rules:numerics
(FPCore (a b c)
  :name "The quadratic formula (r1)"
  :precision binary64

  :herbie-target
  (if (< b 0.0) (/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a)) (/ c (* a (/ (- (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a)))))

  (/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a)))