Average Error: 33.0 → 6.6
Time: 1.6m
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 -9.139254247068609 \cdot 10^{+140}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le 9.632148973470483 \cdot 10^{-296}:\\ \;\;\;\;\frac{1}{2} \cdot \frac{\sqrt{(-4 \cdot \left(c \cdot a\right) + \left(b \cdot b\right))_*} - b}{a}\\ \mathbf{elif}\;b \le 6.523631550102089 \cdot 10^{+89}:\\ \;\;\;\;\frac{\frac{-c}{\left(-b\right) - \sqrt{(\left(a \cdot -4\right) \cdot c + \left(b \cdot b\right))_*}}}{\frac{-1}{2}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \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 -9.139254247068609 \cdot 10^{+140}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \le 9.632148973470483 \cdot 10^{-296}:\\
\;\;\;\;\frac{1}{2} \cdot \frac{\sqrt{(-4 \cdot \left(c \cdot a\right) + \left(b \cdot b\right))_*} - b}{a}\\

\mathbf{elif}\;b \le 6.523631550102089 \cdot 10^{+89}:\\
\;\;\;\;\frac{\frac{-c}{\left(-b\right) - \sqrt{(\left(a \cdot -4\right) \cdot c + \left(b \cdot b\right))_*}}}{\frac{-1}{2}}\\

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

\end{array}
double f(double a, double b, double c) {
        double r31140979 = b;
        double r31140980 = -r31140979;
        double r31140981 = r31140979 * r31140979;
        double r31140982 = 4.0;
        double r31140983 = a;
        double r31140984 = r31140982 * r31140983;
        double r31140985 = c;
        double r31140986 = r31140984 * r31140985;
        double r31140987 = r31140981 - r31140986;
        double r31140988 = sqrt(r31140987);
        double r31140989 = r31140980 + r31140988;
        double r31140990 = 2.0;
        double r31140991 = r31140990 * r31140983;
        double r31140992 = r31140989 / r31140991;
        return r31140992;
}

double f(double a, double b, double c) {
        double r31140993 = b;
        double r31140994 = -9.139254247068609e+140;
        bool r31140995 = r31140993 <= r31140994;
        double r31140996 = c;
        double r31140997 = r31140996 / r31140993;
        double r31140998 = a;
        double r31140999 = r31140993 / r31140998;
        double r31141000 = r31140997 - r31140999;
        double r31141001 = 9.632148973470483e-296;
        bool r31141002 = r31140993 <= r31141001;
        double r31141003 = 0.5;
        double r31141004 = -4.0;
        double r31141005 = r31140996 * r31140998;
        double r31141006 = r31140993 * r31140993;
        double r31141007 = fma(r31141004, r31141005, r31141006);
        double r31141008 = sqrt(r31141007);
        double r31141009 = r31141008 - r31140993;
        double r31141010 = r31141009 / r31140998;
        double r31141011 = r31141003 * r31141010;
        double r31141012 = 6.523631550102089e+89;
        bool r31141013 = r31140993 <= r31141012;
        double r31141014 = -r31140996;
        double r31141015 = -r31140993;
        double r31141016 = r31140998 * r31141004;
        double r31141017 = fma(r31141016, r31140996, r31141006);
        double r31141018 = sqrt(r31141017);
        double r31141019 = r31141015 - r31141018;
        double r31141020 = r31141014 / r31141019;
        double r31141021 = -0.5;
        double r31141022 = r31141020 / r31141021;
        double r31141023 = -r31140997;
        double r31141024 = r31141013 ? r31141022 : r31141023;
        double r31141025 = r31141002 ? r31141011 : r31141024;
        double r31141026 = r31140995 ? r31141000 : r31141025;
        return r31141026;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original33.0
Target20.1
Herbie6.6
\[\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 4 regimes
  2. if b < -9.139254247068609e+140

    1. Initial program 55.8

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

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

    if -9.139254247068609e+140 < b < 9.632148973470483e-296

    1. Initial program 8.7

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity8.7

      \[\leadsto \frac{\left(-b\right) + \color{blue}{1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}{2 \cdot a}\]
    4. Applied *-un-lft-identity8.7

      \[\leadsto \frac{\left(-\color{blue}{1 \cdot b}\right) + 1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    5. Applied distribute-rgt-neg-in8.7

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

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

      \[\leadsto \color{blue}{\frac{1}{\frac{2 \cdot a}{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}}\]
    8. Using strategy rm
    9. Applied *-un-lft-identity8.9

      \[\leadsto \frac{1}{\frac{2 \cdot a}{\color{blue}{1 \cdot \left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}}}\]
    10. Applied times-frac8.9

      \[\leadsto \frac{1}{\color{blue}{\frac{2}{1} \cdot \frac{a}{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}}\]
    11. Applied *-un-lft-identity8.9

      \[\leadsto \frac{\color{blue}{1 \cdot 1}}{\frac{2}{1} \cdot \frac{a}{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    12. Applied times-frac8.9

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

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

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

    if 9.632148973470483e-296 < b < 6.523631550102089e+89

    1. Initial program 32.6

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity32.6

      \[\leadsto \frac{\left(-b\right) + \color{blue}{1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}{2 \cdot a}\]
    4. Applied *-un-lft-identity32.6

      \[\leadsto \frac{\left(-\color{blue}{1 \cdot b}\right) + 1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    5. Applied distribute-rgt-neg-in32.6

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

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

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

      \[\leadsto \frac{1}{\frac{2 \cdot a}{\color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}}}\]
    10. Applied associate-/r/32.8

      \[\leadsto \frac{1}{\color{blue}{\frac{2 \cdot a}{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}} \cdot \left(\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}}\]
    11. Applied *-un-lft-identity32.8

      \[\leadsto \frac{\color{blue}{1 \cdot 1}}{\frac{2 \cdot a}{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}} \cdot \left(\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}\]
    12. Applied times-frac32.8

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

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

      \[\leadsto \frac{-\frac{a \cdot c}{\frac{-1}{2}}}{a} \cdot \color{blue}{\frac{1}{\left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}}\]
    15. Using strategy rm
    16. Applied distribute-frac-neg16.4

      \[\leadsto \color{blue}{\left(-\frac{\frac{a \cdot c}{\frac{-1}{2}}}{a}\right)} \cdot \frac{1}{\left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}\]
    17. Applied distribute-lft-neg-out16.4

      \[\leadsto \color{blue}{-\frac{\frac{a \cdot c}{\frac{-1}{2}}}{a} \cdot \frac{1}{\left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}}\]
    18. Simplified9.3

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

    if 6.523631550102089e+89 < b

    1. Initial program 58.7

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

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

      \[\leadsto \color{blue}{-\frac{c}{b}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -9.139254247068609 \cdot 10^{+140}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le 9.632148973470483 \cdot 10^{-296}:\\ \;\;\;\;\frac{1}{2} \cdot \frac{\sqrt{(-4 \cdot \left(c \cdot a\right) + \left(b \cdot b\right))_*} - b}{a}\\ \mathbf{elif}\;b \le 6.523631550102089 \cdot 10^{+89}:\\ \;\;\;\;\frac{\frac{-c}{\left(-b\right) - \sqrt{(\left(a \cdot -4\right) \cdot c + \left(b \cdot b\right))_*}}}{\frac{-1}{2}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array}\]

Reproduce

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