Average Error: 34.9 → 12.1
Time: 19.0s
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 -3.938820754703495889486844390187078997042 \cdot 10^{91}:\\ \;\;\;\;\frac{\left(-b\right) + \left(2 \cdot \frac{a \cdot c}{b} - b\right)}{2 \cdot a}\\ \mathbf{elif}\;b \le 1.769241967981359063235122492290664188248 \cdot 10^{-128}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-\sqrt[3]{b} \cdot \sqrt[3]{b}, \sqrt[3]{b}, \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}\\ \mathbf{elif}\;b \le 2.197476988225998234030618417824689258562 \cdot 10^{149}:\\ \;\;\;\;\frac{\frac{\left(a \cdot c\right) \cdot 4}{2 \cdot a}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-2 \cdot \frac{a \cdot c}{b}}{2 \cdot a}\\ \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 -3.938820754703495889486844390187078997042 \cdot 10^{91}:\\
\;\;\;\;\frac{\left(-b\right) + \left(2 \cdot \frac{a \cdot c}{b} - b\right)}{2 \cdot a}\\

\mathbf{elif}\;b \le 1.769241967981359063235122492290664188248 \cdot 10^{-128}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-\sqrt[3]{b} \cdot \sqrt[3]{b}, \sqrt[3]{b}, \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}\\

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

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

\end{array}
double f(double a, double b, double c) {
        double r187313 = b;
        double r187314 = -r187313;
        double r187315 = r187313 * r187313;
        double r187316 = 4.0;
        double r187317 = a;
        double r187318 = r187316 * r187317;
        double r187319 = c;
        double r187320 = r187318 * r187319;
        double r187321 = r187315 - r187320;
        double r187322 = sqrt(r187321);
        double r187323 = r187314 + r187322;
        double r187324 = 2.0;
        double r187325 = r187324 * r187317;
        double r187326 = r187323 / r187325;
        return r187326;
}

double f(double a, double b, double c) {
        double r187327 = b;
        double r187328 = -3.938820754703496e+91;
        bool r187329 = r187327 <= r187328;
        double r187330 = -r187327;
        double r187331 = 2.0;
        double r187332 = a;
        double r187333 = c;
        double r187334 = r187332 * r187333;
        double r187335 = r187334 / r187327;
        double r187336 = r187331 * r187335;
        double r187337 = r187336 - r187327;
        double r187338 = r187330 + r187337;
        double r187339 = r187331 * r187332;
        double r187340 = r187338 / r187339;
        double r187341 = 1.769241967981359e-128;
        bool r187342 = r187327 <= r187341;
        double r187343 = cbrt(r187327);
        double r187344 = r187343 * r187343;
        double r187345 = -r187344;
        double r187346 = r187327 * r187327;
        double r187347 = 4.0;
        double r187348 = r187347 * r187332;
        double r187349 = r187348 * r187333;
        double r187350 = r187346 - r187349;
        double r187351 = sqrt(r187350);
        double r187352 = fma(r187345, r187343, r187351);
        double r187353 = r187352 / r187339;
        double r187354 = 2.1974769882259982e+149;
        bool r187355 = r187327 <= r187354;
        double r187356 = r187334 * r187347;
        double r187357 = r187356 / r187339;
        double r187358 = r187330 - r187351;
        double r187359 = r187357 / r187358;
        double r187360 = -2.0;
        double r187361 = r187360 * r187335;
        double r187362 = r187361 / r187339;
        double r187363 = r187355 ? r187359 : r187362;
        double r187364 = r187342 ? r187353 : r187363;
        double r187365 = r187329 ? r187340 : r187364;
        return r187365;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.9
Target21.3
Herbie12.1
\[\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 < -3.938820754703496e+91

    1. Initial program 45.7

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

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

    if -3.938820754703496e+91 < b < 1.769241967981359e-128

    1. Initial program 11.8

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

      \[\leadsto \frac{\left(-\color{blue}{\left(\sqrt[3]{b} \cdot \sqrt[3]{b}\right) \cdot \sqrt[3]{b}}\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    4. Applied distribute-lft-neg-in12.0

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

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

    if 1.769241967981359e-128 < b < 2.1974769882259982e+149

    1. Initial program 42.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 flip-+42.7

      \[\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. Simplified15.2

      \[\leadsto \frac{\frac{\color{blue}{0 + c \cdot \left(4 \cdot a\right)}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}{2 \cdot a}\]
    5. Using strategy rm
    6. Applied div-inv15.3

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

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

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

    if 2.1974769882259982e+149 < b

    1. Initial program 63.6

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -3.938820754703495889486844390187078997042 \cdot 10^{91}:\\ \;\;\;\;\frac{\left(-b\right) + \left(2 \cdot \frac{a \cdot c}{b} - b\right)}{2 \cdot a}\\ \mathbf{elif}\;b \le 1.769241967981359063235122492290664188248 \cdot 10^{-128}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-\sqrt[3]{b} \cdot \sqrt[3]{b}, \sqrt[3]{b}, \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}\\ \mathbf{elif}\;b \le 2.197476988225998234030618417824689258562 \cdot 10^{149}:\\ \;\;\;\;\frac{\frac{\left(a \cdot c\right) \cdot 4}{2 \cdot a}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-2 \cdot \frac{a \cdot c}{b}}{2 \cdot a}\\ \end{array}\]

Reproduce

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

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