Average Error: 34.1 → 10.3
Time: 5.7s
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 -2.3165289123951838 \cdot 10^{-123}:\\ \;\;\;\;1 \cdot \left(-1 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \le 6.7711658147871089 \cdot 10^{125}:\\ \;\;\;\;1 \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt[3]{b} \cdot \sqrt[3]{b}}, \frac{\sqrt[3]{c}}{\sqrt[3]{b}}, -\frac{b}{a}\right)\\ \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 -2.3165289123951838 \cdot 10^{-123}:\\
\;\;\;\;1 \cdot \left(-1 \cdot \frac{c}{b}\right)\\

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

\mathbf{else}:\\
\;\;\;\;1 \cdot \mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt[3]{b} \cdot \sqrt[3]{b}}, \frac{\sqrt[3]{c}}{\sqrt[3]{b}}, -\frac{b}{a}\right)\\

\end{array}
double f(double a, double b, double c) {
        double r88438 = b;
        double r88439 = -r88438;
        double r88440 = r88438 * r88438;
        double r88441 = 4.0;
        double r88442 = a;
        double r88443 = c;
        double r88444 = r88442 * r88443;
        double r88445 = r88441 * r88444;
        double r88446 = r88440 - r88445;
        double r88447 = sqrt(r88446);
        double r88448 = r88439 - r88447;
        double r88449 = 2.0;
        double r88450 = r88449 * r88442;
        double r88451 = r88448 / r88450;
        return r88451;
}

double f(double a, double b, double c) {
        double r88452 = b;
        double r88453 = -2.3165289123951838e-123;
        bool r88454 = r88452 <= r88453;
        double r88455 = 1.0;
        double r88456 = -1.0;
        double r88457 = c;
        double r88458 = r88457 / r88452;
        double r88459 = r88456 * r88458;
        double r88460 = r88455 * r88459;
        double r88461 = 6.771165814787109e+125;
        bool r88462 = r88452 <= r88461;
        double r88463 = -r88452;
        double r88464 = r88452 * r88452;
        double r88465 = 4.0;
        double r88466 = a;
        double r88467 = r88466 * r88457;
        double r88468 = r88465 * r88467;
        double r88469 = r88464 - r88468;
        double r88470 = sqrt(r88469);
        double r88471 = r88463 - r88470;
        double r88472 = 2.0;
        double r88473 = r88472 * r88466;
        double r88474 = r88471 / r88473;
        double r88475 = r88455 * r88474;
        double r88476 = 1.0;
        double r88477 = cbrt(r88457);
        double r88478 = r88477 * r88477;
        double r88479 = cbrt(r88452);
        double r88480 = r88479 * r88479;
        double r88481 = r88478 / r88480;
        double r88482 = r88477 / r88479;
        double r88483 = r88452 / r88466;
        double r88484 = -r88483;
        double r88485 = fma(r88481, r88482, r88484);
        double r88486 = r88476 * r88485;
        double r88487 = r88462 ? r88475 : r88486;
        double r88488 = r88454 ? r88460 : r88487;
        return r88488;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.1
Target21.2
Herbie10.3
\[\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 3 regimes
  2. if b < -2.3165289123951838e-123

    1. Initial program 51.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-inv51.0

      \[\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 *-un-lft-identity51.0

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

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

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

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

    if -2.3165289123951838e-123 < b < 6.771165814787109e+125

    1. Initial program 11.4

      \[\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-inv11.5

      \[\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 *-un-lft-identity11.5

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

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

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

    if 6.771165814787109e+125 < b

    1. Initial program 53.5

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

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

      \[\leadsto \color{blue}{1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)}\]
    4. Using strategy rm
    5. Applied add-cube-cbrt2.5

      \[\leadsto 1 \cdot \left(\frac{c}{\color{blue}{\left(\sqrt[3]{b} \cdot \sqrt[3]{b}\right) \cdot \sqrt[3]{b}}} - \frac{b}{a}\right)\]
    6. Applied add-cube-cbrt2.5

      \[\leadsto 1 \cdot \left(\frac{\color{blue}{\left(\sqrt[3]{c} \cdot \sqrt[3]{c}\right) \cdot \sqrt[3]{c}}}{\left(\sqrt[3]{b} \cdot \sqrt[3]{b}\right) \cdot \sqrt[3]{b}} - \frac{b}{a}\right)\]
    7. Applied times-frac2.5

      \[\leadsto 1 \cdot \left(\color{blue}{\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt[3]{b} \cdot \sqrt[3]{b}} \cdot \frac{\sqrt[3]{c}}{\sqrt[3]{b}}} - \frac{b}{a}\right)\]
    8. Applied fma-neg2.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -2.3165289123951838 \cdot 10^{-123}:\\ \;\;\;\;1 \cdot \left(-1 \cdot \frac{c}{b}\right)\\ \mathbf{elif}\;b \le 6.7711658147871089 \cdot 10^{125}:\\ \;\;\;\;1 \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;1 \cdot \mathsf{fma}\left(\frac{\sqrt[3]{c} \cdot \sqrt[3]{c}}{\sqrt[3]{b} \cdot \sqrt[3]{b}}, \frac{\sqrt[3]{c}}{\sqrt[3]{b}}, -\frac{b}{a}\right)\\ \end{array}\]

Reproduce

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

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

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