Average Error: 33.0 → 6.6
Time: 1.3m
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 r24045566 = b;
        double r24045567 = -r24045566;
        double r24045568 = r24045566 * r24045566;
        double r24045569 = 4.0;
        double r24045570 = a;
        double r24045571 = r24045569 * r24045570;
        double r24045572 = c;
        double r24045573 = r24045571 * r24045572;
        double r24045574 = r24045568 - r24045573;
        double r24045575 = sqrt(r24045574);
        double r24045576 = r24045567 + r24045575;
        double r24045577 = 2.0;
        double r24045578 = r24045577 * r24045570;
        double r24045579 = r24045576 / r24045578;
        return r24045579;
}

double f(double a, double b, double c) {
        double r24045580 = b;
        double r24045581 = -9.139254247068609e+140;
        bool r24045582 = r24045580 <= r24045581;
        double r24045583 = c;
        double r24045584 = r24045583 / r24045580;
        double r24045585 = a;
        double r24045586 = r24045580 / r24045585;
        double r24045587 = r24045584 - r24045586;
        double r24045588 = 9.632148973470483e-296;
        bool r24045589 = r24045580 <= r24045588;
        double r24045590 = 0.5;
        double r24045591 = -4.0;
        double r24045592 = r24045583 * r24045585;
        double r24045593 = r24045580 * r24045580;
        double r24045594 = fma(r24045591, r24045592, r24045593);
        double r24045595 = sqrt(r24045594);
        double r24045596 = r24045595 - r24045580;
        double r24045597 = r24045596 / r24045585;
        double r24045598 = r24045590 * r24045597;
        double r24045599 = 6.523631550102089e+89;
        bool r24045600 = r24045580 <= r24045599;
        double r24045601 = -r24045583;
        double r24045602 = -r24045580;
        double r24045603 = r24045585 * r24045591;
        double r24045604 = fma(r24045603, r24045583, r24045593);
        double r24045605 = sqrt(r24045604);
        double r24045606 = r24045602 - r24045605;
        double r24045607 = r24045601 / r24045606;
        double r24045608 = -0.5;
        double r24045609 = r24045607 / r24045608;
        double r24045610 = -r24045584;
        double r24045611 = r24045600 ? r24045609 : r24045610;
        double r24045612 = r24045589 ? r24045598 : r24045611;
        double r24045613 = r24045582 ? r24045587 : r24045612;
        return r24045613;
}

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{\color{blue}{1 \cdot \left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}}{2 \cdot a}\]
    4. 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}}}}\]
    5. Using strategy rm
    6. Applied *-un-lft-identity8.9

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

      \[\leadsto \frac{1}{\frac{2 \cdot a}{\color{blue}{1 \cdot \left(-b\right)} + 1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    8. Applied distribute-lft-out8.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)}}}\]
    9. 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}}}}\]
    10. Applied add-sqr-sqrt8.9

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

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

      \[\leadsto \color{blue}{\frac{1}{2}} \cdot \frac{\sqrt{1}}{\frac{a}{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    13. 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{\color{blue}{1 \cdot \left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}}{2 \cdot a}\]
    4. 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}}}}\]
    5. Using strategy rm
    6. 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}}}}}\]
    7. 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)}}\]
    8. 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)}\]
    9. 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}}}\]
    10. 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}}\]
    11. 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))_*}}}\]
    12. Using strategy rm
    13. 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))_*}}\]
    14. 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))_*}}}\]
    15. 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)))