Average Error: 34.2 → 7.2
Time: 16.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 -4.065832835277420955864657569887710364937 \cdot 10^{109}:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \mathbf{elif}\;b \le -4.565615297402513793822059617442033581563 \cdot 10^{-201}:\\ \;\;\;\;\left(\sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)} + \left(-b\right)\right) \cdot \frac{1}{a \cdot 2}\\ \mathbf{elif}\;b \le 4.287176096066254708923845932443784224798 \cdot 10^{77}:\\ \;\;\;\;\frac{1}{2} \cdot \frac{\frac{1}{\frac{1}{4 \cdot c}}}{\left(-b\right) - \sqrt{\sqrt{b \cdot b - \left(4 \cdot c\right) \cdot a}} \cdot \sqrt{\sqrt{b \cdot b - \left(4 \cdot c\right) \cdot a}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} \cdot -1\\ \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.065832835277420955864657569887710364937 \cdot 10^{109}:\\
\;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\

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

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

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

\end{array}
double f(double a, double b, double c) {
        double r161023 = b;
        double r161024 = -r161023;
        double r161025 = r161023 * r161023;
        double r161026 = 4.0;
        double r161027 = a;
        double r161028 = r161026 * r161027;
        double r161029 = c;
        double r161030 = r161028 * r161029;
        double r161031 = r161025 - r161030;
        double r161032 = sqrt(r161031);
        double r161033 = r161024 + r161032;
        double r161034 = 2.0;
        double r161035 = r161034 * r161027;
        double r161036 = r161033 / r161035;
        return r161036;
}

double f(double a, double b, double c) {
        double r161037 = b;
        double r161038 = -4.065832835277421e+109;
        bool r161039 = r161037 <= r161038;
        double r161040 = c;
        double r161041 = r161040 / r161037;
        double r161042 = a;
        double r161043 = r161037 / r161042;
        double r161044 = r161041 - r161043;
        double r161045 = 1.0;
        double r161046 = r161044 * r161045;
        double r161047 = -4.565615297402514e-201;
        bool r161048 = r161037 <= r161047;
        double r161049 = r161037 * r161037;
        double r161050 = 4.0;
        double r161051 = r161050 * r161042;
        double r161052 = r161040 * r161051;
        double r161053 = r161049 - r161052;
        double r161054 = sqrt(r161053);
        double r161055 = -r161037;
        double r161056 = r161054 + r161055;
        double r161057 = 1.0;
        double r161058 = 2.0;
        double r161059 = r161042 * r161058;
        double r161060 = r161057 / r161059;
        double r161061 = r161056 * r161060;
        double r161062 = 4.2871760960662547e+77;
        bool r161063 = r161037 <= r161062;
        double r161064 = r161057 / r161058;
        double r161065 = r161050 * r161040;
        double r161066 = r161057 / r161065;
        double r161067 = r161057 / r161066;
        double r161068 = r161065 * r161042;
        double r161069 = r161049 - r161068;
        double r161070 = sqrt(r161069);
        double r161071 = sqrt(r161070);
        double r161072 = r161071 * r161071;
        double r161073 = r161055 - r161072;
        double r161074 = r161067 / r161073;
        double r161075 = r161064 * r161074;
        double r161076 = -1.0;
        double r161077 = r161041 * r161076;
        double r161078 = r161063 ? r161075 : r161077;
        double r161079 = r161048 ? r161061 : r161078;
        double r161080 = r161039 ? r161046 : r161079;
        return r161080;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original34.2
Target21.0
Herbie7.2
\[\begin{array}{l} \mathbf{if}\;b \lt 0.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 < -4.065832835277421e+109

    1. Initial program 49.5

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

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

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

    if -4.065832835277421e+109 < b < -4.565615297402514e-201

    1. Initial program 8.1

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv8.2

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

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

    if -4.565615297402514e-201 < b < 4.2871760960662547e+77

    1. Initial program 28.4

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied flip-+28.5

      \[\leadsto \frac{\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}}}}{2 \cdot a}\]
    4. Simplified16.7

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

      \[\leadsto \frac{\frac{0 + \left(4 \cdot c\right) \cdot a}{\color{blue}{\left(-b\right) - \sqrt{\mathsf{fma}\left(a \cdot c, -4, b \cdot b\right)}}}}{2 \cdot a}\]
    6. Using strategy rm
    7. Applied *-un-lft-identity16.7

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

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

      \[\leadsto \frac{\color{blue}{\frac{1}{1} \cdot \frac{0 + \left(4 \cdot c\right) \cdot a}{\left(-b\right) - \sqrt{\mathsf{fma}\left(a \cdot c, -4, b \cdot b\right)}}}}{2 \cdot a}\]
    10. Applied times-frac16.7

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

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

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

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

      \[\leadsto \frac{1}{2} \cdot \frac{\frac{1}{\color{blue}{\frac{1}{4 \cdot c}}}}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(-a\right) \cdot \left(4 \cdot c\right)\right)}}\]
    16. Using strategy rm
    17. Applied add-sqr-sqrt10.3

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

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

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

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

    if 4.2871760960662547e+77 < b

    1. Initial program 58.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -4.065832835277420955864657569887710364937 \cdot 10^{109}:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \mathbf{elif}\;b \le -4.565615297402513793822059617442033581563 \cdot 10^{-201}:\\ \;\;\;\;\left(\sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)} + \left(-b\right)\right) \cdot \frac{1}{a \cdot 2}\\ \mathbf{elif}\;b \le 4.287176096066254708923845932443784224798 \cdot 10^{77}:\\ \;\;\;\;\frac{1}{2} \cdot \frac{\frac{1}{\frac{1}{4 \cdot c}}}{\left(-b\right) - \sqrt{\sqrt{b \cdot b - \left(4 \cdot c\right) \cdot a}} \cdot \sqrt{\sqrt{b \cdot b - \left(4 \cdot c\right) \cdot a}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} \cdot -1\\ \end{array}\]

Reproduce

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

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

  (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))