Average Error: 33.4 → 16.7
Time: 21.9s
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 8.670930634061063 \cdot 10^{-143}:\\ \;\;\;\;\frac{\left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right) \cdot \frac{1}{2}}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{-\frac{b}{c}}\\ \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 8.670930634061063 \cdot 10^{-143}:\\
\;\;\;\;\frac{\left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right) \cdot \frac{1}{2}}{a}\\

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

\end{array}
double f(double a, double b, double c) {
        double r2368768 = b;
        double r2368769 = -r2368768;
        double r2368770 = r2368768 * r2368768;
        double r2368771 = 4.0;
        double r2368772 = a;
        double r2368773 = r2368771 * r2368772;
        double r2368774 = c;
        double r2368775 = r2368773 * r2368774;
        double r2368776 = r2368770 - r2368775;
        double r2368777 = sqrt(r2368776);
        double r2368778 = r2368769 + r2368777;
        double r2368779 = 2.0;
        double r2368780 = r2368779 * r2368772;
        double r2368781 = r2368778 / r2368780;
        return r2368781;
}

double f(double a, double b, double c) {
        double r2368782 = b;
        double r2368783 = 8.670930634061063e-143;
        bool r2368784 = r2368782 <= r2368783;
        double r2368785 = c;
        double r2368786 = a;
        double r2368787 = -4.0;
        double r2368788 = r2368786 * r2368787;
        double r2368789 = r2368782 * r2368782;
        double r2368790 = fma(r2368785, r2368788, r2368789);
        double r2368791 = sqrt(r2368790);
        double r2368792 = r2368791 - r2368782;
        double r2368793 = 0.5;
        double r2368794 = r2368792 * r2368793;
        double r2368795 = r2368794 / r2368786;
        double r2368796 = 1.0;
        double r2368797 = r2368782 / r2368785;
        double r2368798 = -r2368797;
        double r2368799 = r2368796 / r2368798;
        double r2368800 = r2368784 ? r2368795 : r2368799;
        return r2368800;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original33.4
Target19.8
Herbie16.7
\[\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 < 8.670930634061063e-143

    1. Initial program 20.2

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

      \[\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 *-un-lft-identity20.2

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

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

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

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

      \[\leadsto \left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right) \cdot \color{blue}{\frac{\frac{1}{2}}{a}}\]
    9. Using strategy rm
    10. Applied associate-*r/20.2

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

    if 8.670930634061063e-143 < b

    1. Initial program 49.8

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

      \[\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 *-un-lft-identity49.8

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

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

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

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

      \[\leadsto \left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right) \cdot \color{blue}{\frac{\frac{1}{2}}{a}}\]
    9. Using strategy rm
    10. Applied associate-*r/49.8

      \[\leadsto \color{blue}{\frac{\left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right) \cdot \frac{1}{2}}{a}}\]
    11. Using strategy rm
    12. Applied clear-num49.9

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

      \[\leadsto \frac{1}{\color{blue}{-1 \cdot \frac{b}{c}}}\]
    14. Simplified12.3

      \[\leadsto \frac{1}{\color{blue}{-\frac{b}{c}}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification16.7

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

Reproduce

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