Average Error: 34.0 → 29.9
Time: 1.1m
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 4.1432508585428765 \cdot 10^{+166}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2} \cdot \frac{1}{a}\\ \mathbf{else}:\\ \;\;\;\;0\\ \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 4.1432508585428765 \cdot 10^{+166}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2} \cdot \frac{1}{a}\\

\mathbf{else}:\\
\;\;\;\;0\\

\end{array}
double f(double a, double b, double c) {
        double r5863990 = b;
        double r5863991 = -r5863990;
        double r5863992 = r5863990 * r5863990;
        double r5863993 = 4.0;
        double r5863994 = a;
        double r5863995 = r5863993 * r5863994;
        double r5863996 = c;
        double r5863997 = r5863995 * r5863996;
        double r5863998 = r5863992 - r5863997;
        double r5863999 = sqrt(r5863998);
        double r5864000 = r5863991 + r5863999;
        double r5864001 = 2.0;
        double r5864002 = r5864001 * r5863994;
        double r5864003 = r5864000 / r5864002;
        return r5864003;
}

double f(double a, double b, double c) {
        double r5864004 = b;
        double r5864005 = 4.1432508585428765e+166;
        bool r5864006 = r5864004 <= r5864005;
        double r5864007 = c;
        double r5864008 = -4.0;
        double r5864009 = a;
        double r5864010 = r5864008 * r5864009;
        double r5864011 = r5864004 * r5864004;
        double r5864012 = fma(r5864007, r5864010, r5864011);
        double r5864013 = sqrt(r5864012);
        double r5864014 = r5864013 - r5864004;
        double r5864015 = 2.0;
        double r5864016 = r5864014 / r5864015;
        double r5864017 = 1.0;
        double r5864018 = r5864017 / r5864009;
        double r5864019 = r5864016 * r5864018;
        double r5864020 = 0.0;
        double r5864021 = r5864006 ? r5864019 : r5864020;
        return r5864021;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.0
Target20.7
Herbie29.9
\[\begin{array}{l} \mathbf{if}\;b \lt 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 2 regimes
  2. if b < 4.1432508585428765e+166

    1. Initial program 28.3

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

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

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

    if 4.1432508585428765e+166 < b

    1. Initial program 62.9

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

      \[\leadsto \color{blue}{\frac{\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2}}{a}}\]
    3. Taylor expanded around 0 37.8

      \[\leadsto \color{blue}{0}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification29.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le 4.1432508585428765 \cdot 10^{+166}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2} \cdot \frac{1}{a}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array}\]

Reproduce

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

  :herbie-target
  (if (< b 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)))