Average Error: 34.2 → 6.5
Time: 31.5s
Precision: 64
\[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \le -1.650716803435188882222807352588915693143 \cdot 10^{100}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \mathbf{elif}\;b \le 1.586183022085898567784328170731092391431 \cdot 10^{-203}:\\ \;\;\;\;\frac{2 \cdot c}{\sqrt{b \cdot b - \left(c \cdot a\right) \cdot 4} + \left(-b\right)}\\ \mathbf{elif}\;b \le 6.994525771494005489896684232081336893823 \cdot 10^{142}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot \left(-4\right)\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \end{array}\]
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \le -1.650716803435188882222807352588915693143 \cdot 10^{100}:\\
\;\;\;\;-1 \cdot \frac{c}{b}\\

\mathbf{elif}\;b \le 1.586183022085898567784328170731092391431 \cdot 10^{-203}:\\
\;\;\;\;\frac{2 \cdot c}{\sqrt{b \cdot b - \left(c \cdot a\right) \cdot 4} + \left(-b\right)}\\

\mathbf{elif}\;b \le 6.994525771494005489896684232081336893823 \cdot 10^{142}:\\
\;\;\;\;\frac{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot \left(-4\right)\right)}}{2 \cdot a}\\

\mathbf{else}:\\
\;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\

\end{array}
double f(double a, double b, double c) {
        double r3784494 = b;
        double r3784495 = -r3784494;
        double r3784496 = r3784494 * r3784494;
        double r3784497 = 4.0;
        double r3784498 = a;
        double r3784499 = c;
        double r3784500 = r3784498 * r3784499;
        double r3784501 = r3784497 * r3784500;
        double r3784502 = r3784496 - r3784501;
        double r3784503 = sqrt(r3784502);
        double r3784504 = r3784495 - r3784503;
        double r3784505 = 2.0;
        double r3784506 = r3784505 * r3784498;
        double r3784507 = r3784504 / r3784506;
        return r3784507;
}

double f(double a, double b, double c) {
        double r3784508 = b;
        double r3784509 = -1.6507168034351889e+100;
        bool r3784510 = r3784508 <= r3784509;
        double r3784511 = -1.0;
        double r3784512 = c;
        double r3784513 = r3784512 / r3784508;
        double r3784514 = r3784511 * r3784513;
        double r3784515 = 1.5861830220858986e-203;
        bool r3784516 = r3784508 <= r3784515;
        double r3784517 = 2.0;
        double r3784518 = r3784517 * r3784512;
        double r3784519 = r3784508 * r3784508;
        double r3784520 = a;
        double r3784521 = r3784512 * r3784520;
        double r3784522 = 4.0;
        double r3784523 = r3784521 * r3784522;
        double r3784524 = r3784519 - r3784523;
        double r3784525 = sqrt(r3784524);
        double r3784526 = -r3784508;
        double r3784527 = r3784525 + r3784526;
        double r3784528 = r3784518 / r3784527;
        double r3784529 = 6.994525771494005e+142;
        bool r3784530 = r3784508 <= r3784529;
        double r3784531 = -r3784522;
        double r3784532 = r3784521 * r3784531;
        double r3784533 = fma(r3784508, r3784508, r3784532);
        double r3784534 = sqrt(r3784533);
        double r3784535 = r3784526 - r3784534;
        double r3784536 = r3784517 * r3784520;
        double r3784537 = r3784535 / r3784536;
        double r3784538 = r3784508 / r3784520;
        double r3784539 = r3784513 - r3784538;
        double r3784540 = 1.0;
        double r3784541 = r3784539 * r3784540;
        double r3784542 = r3784530 ? r3784537 : r3784541;
        double r3784543 = r3784516 ? r3784528 : r3784542;
        double r3784544 = r3784510 ? r3784514 : r3784543;
        return r3784544;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.2
Target21.1
Herbie6.5
\[\begin{array}{l} \mathbf{if}\;b \lt 0.0:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \end{array}\]

Derivation

  1. Split input into 4 regimes
  2. if b < -1.6507168034351889e+100

    1. Initial program 59.6

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Taylor expanded around -inf 2.8

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}}\]

    if -1.6507168034351889e+100 < b < 1.5861830220858986e-203

    1. Initial program 30.0

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv30.1

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

      \[\leadsto \color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} \cdot \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}} \cdot \frac{1}{2 \cdot a}\]
    6. Applied associate-*l/30.2

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

      \[\leadsto \frac{\color{blue}{\frac{\frac{\left(b \cdot b - b \cdot b\right) + \left(c \cdot a\right) \cdot 4}{2}}{a}}}{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}\]
    8. Taylor expanded around 0 9.8

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

    if 1.5861830220858986e-203 < b < 6.994525771494005e+142

    1. Initial program 7.3

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied fma-neg7.3

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

    if 6.994525771494005e+142 < b

    1. Initial program 59.9

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv59.9

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

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

      \[\leadsto \color{blue}{1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -1.650716803435188882222807352588915693143 \cdot 10^{100}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \mathbf{elif}\;b \le 1.586183022085898567784328170731092391431 \cdot 10^{-203}:\\ \;\;\;\;\frac{2 \cdot c}{\sqrt{b \cdot b - \left(c \cdot a\right) \cdot 4} + \left(-b\right)}\\ \mathbf{elif}\;b \le 6.994525771494005489896684232081336893823 \cdot 10^{142}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot \left(-4\right)\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \end{array}\]

Reproduce

herbie shell --seed 2019200 +o rules:numerics
(FPCore (a b c)
  :name "The quadratic formula (r2)"

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

  (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))