Average Error: 33.8 → 6.7
Time: 1.4m
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.3671561050226844 \cdot 10^{+101}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le 5.6646816643665726 \cdot 10^{-285}:\\ \;\;\;\;\left(\left(-b\right) + \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}\right) \cdot \frac{1}{a \cdot 2}\\ \mathbf{elif}\;b \le 2.8546456093447043 \cdot 10^{+107}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{2 \cdot \left(\frac{c}{b} \cdot a - b\right)}\\ \end{array}\]
double f(double a, double b, double c) {
        double r10668654 = b;
        double r10668655 = -r10668654;
        double r10668656 = r10668654 * r10668654;
        double r10668657 = 4.0;
        double r10668658 = a;
        double r10668659 = r10668657 * r10668658;
        double r10668660 = c;
        double r10668661 = r10668659 * r10668660;
        double r10668662 = r10668656 - r10668661;
        double r10668663 = sqrt(r10668662);
        double r10668664 = r10668655 + r10668663;
        double r10668665 = 2.0;
        double r10668666 = r10668665 * r10668658;
        double r10668667 = r10668664 / r10668666;
        return r10668667;
}

double f(double a, double b, double c) {
        double r10668668 = b;
        double r10668669 = -4.3671561050226844e+101;
        bool r10668670 = r10668668 <= r10668669;
        double r10668671 = c;
        double r10668672 = r10668671 / r10668668;
        double r10668673 = a;
        double r10668674 = r10668668 / r10668673;
        double r10668675 = r10668672 - r10668674;
        double r10668676 = 5.6646816643665726e-285;
        bool r10668677 = r10668668 <= r10668676;
        double r10668678 = -r10668668;
        double r10668679 = r10668668 * r10668668;
        double r10668680 = 4.0;
        double r10668681 = r10668680 * r10668673;
        double r10668682 = r10668671 * r10668681;
        double r10668683 = r10668679 - r10668682;
        double r10668684 = sqrt(r10668683);
        double r10668685 = r10668678 + r10668684;
        double r10668686 = 1.0;
        double r10668687 = 2.0;
        double r10668688 = r10668673 * r10668687;
        double r10668689 = r10668686 / r10668688;
        double r10668690 = r10668685 * r10668689;
        double r10668691 = 2.8546456093447043e+107;
        bool r10668692 = r10668668 <= r10668691;
        double r10668693 = r10668671 * r10668687;
        double r10668694 = r10668678 - r10668684;
        double r10668695 = r10668693 / r10668694;
        double r10668696 = r10668672 * r10668673;
        double r10668697 = r10668696 - r10668668;
        double r10668698 = r10668687 * r10668697;
        double r10668699 = r10668693 / r10668698;
        double r10668700 = r10668692 ? r10668695 : r10668699;
        double r10668701 = r10668677 ? r10668690 : r10668700;
        double r10668702 = r10668670 ? r10668675 : r10668701;
        return r10668702;
}

\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.3671561050226844 \cdot 10^{+101}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

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

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

\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{2 \cdot \left(\frac{c}{b} \cdot a - b\right)}\\

\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original33.8
Target21.0
Herbie6.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 4 regimes
  2. if b < -4.3671561050226844e+101

    1. Initial program 44.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 div-inv44.8

      \[\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. Taylor expanded around -inf 3.8

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

    if -4.3671561050226844e+101 < b < 5.6646816643665726e-285

    1. Initial program 9.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 div-inv9.7

      \[\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}}\]

    if 5.6646816643665726e-285 < b < 2.8546456093447043e+107

    1. Initial program 33.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 div-inv33.5

      \[\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. Using strategy rm
    5. Applied flip-+33.6

      \[\leadsto \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}}} \cdot \frac{1}{2 \cdot a}\]
    6. Applied associate-*l/33.6

      \[\leadsto \color{blue}{\frac{\left(\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}\right) \cdot \frac{1}{2 \cdot a}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    7. Simplified16.0

      \[\leadsto \frac{\color{blue}{-\frac{\frac{c \cdot a}{\frac{-1}{2}}}{a}}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\]
    8. Taylor expanded around -inf 8.7

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

    if 2.8546456093447043e+107 < b

    1. Initial program 59.5

      \[\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-inv59.5

      \[\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. Using strategy rm
    5. Applied flip-+59.6

      \[\leadsto \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}}} \cdot \frac{1}{2 \cdot a}\]
    6. Applied associate-*l/59.6

      \[\leadsto \color{blue}{\frac{\left(\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}\right) \cdot \frac{1}{2 \cdot a}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    7. Simplified32.0

      \[\leadsto \frac{\color{blue}{-\frac{\frac{c \cdot a}{\frac{-1}{2}}}{a}}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\]
    8. Taylor expanded around -inf 31.4

      \[\leadsto \frac{-\color{blue}{-2 \cdot c}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\]
    9. Taylor expanded around inf 6.5

      \[\leadsto \frac{--2 \cdot c}{\color{blue}{2 \cdot \frac{a \cdot c}{b} - 2 \cdot b}}\]
    10. Simplified2.1

      \[\leadsto \frac{--2 \cdot c}{\color{blue}{\left(\frac{c}{b} \cdot a - b\right) \cdot 2}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -4.3671561050226844 \cdot 10^{+101}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le 5.6646816643665726 \cdot 10^{-285}:\\ \;\;\;\;\left(\left(-b\right) + \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}\right) \cdot \frac{1}{a \cdot 2}\\ \mathbf{elif}\;b \le 2.8546456093447043 \cdot 10^{+107}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{2 \cdot \left(\frac{c}{b} \cdot a - b\right)}\\ \end{array}\]

Reproduce

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