Average Error: 34.0 → 30.1
Time: 47.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.599460360788912 \cdot 10^{+41}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\left(\sqrt{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)}}\right), \left(\sqrt{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)}}\right), \left(-b\right)\right)}{2}}{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 1.599460360788912 \cdot 10^{+41}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\left(\sqrt{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)}}\right), \left(\sqrt{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)}}\right), \left(-b\right)\right)}{2}}{a}\\

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

\end{array}
double f(double a, double b, double c) {
        double r15321847 = b;
        double r15321848 = -r15321847;
        double r15321849 = r15321847 * r15321847;
        double r15321850 = 4.0;
        double r15321851 = a;
        double r15321852 = r15321850 * r15321851;
        double r15321853 = c;
        double r15321854 = r15321852 * r15321853;
        double r15321855 = r15321849 - r15321854;
        double r15321856 = sqrt(r15321855);
        double r15321857 = r15321848 + r15321856;
        double r15321858 = 2.0;
        double r15321859 = r15321858 * r15321851;
        double r15321860 = r15321857 / r15321859;
        return r15321860;
}

double f(double a, double b, double c) {
        double r15321861 = b;
        double r15321862 = 1.599460360788912e+41;
        bool r15321863 = r15321861 <= r15321862;
        double r15321864 = c;
        double r15321865 = -4.0;
        double r15321866 = a;
        double r15321867 = r15321865 * r15321866;
        double r15321868 = r15321861 * r15321861;
        double r15321869 = fma(r15321864, r15321867, r15321868);
        double r15321870 = sqrt(r15321869);
        double r15321871 = sqrt(r15321870);
        double r15321872 = -r15321861;
        double r15321873 = fma(r15321871, r15321871, r15321872);
        double r15321874 = 2.0;
        double r15321875 = r15321873 / r15321874;
        double r15321876 = r15321875 / r15321866;
        double r15321877 = 0.0;
        double r15321878 = r15321863 ? r15321876 : r15321877;
        return r15321878;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.0
Target20.9
Herbie30.1
\[\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 < 1.599460360788912e+41

    1. Initial program 24.6

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

      \[\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 add-sqr-sqrt24.9

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

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

    if 1.599460360788912e+41 < b

    1. Initial program 56.3

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

      \[\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 42.6

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

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

Reproduce

herbie shell --seed 2019120 +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)))