Average Error: 34.4 → 10.7
Time: 21.4s
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 -2.221067196710922123169723133116561516447 \cdot 10^{149}:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \mathbf{elif}\;b \le 2.898348930695269343280527497904161468201 \cdot 10^{-35}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\sqrt{\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4}}, \sqrt{\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4}}, -b\right)}{2}}{a}\\ \mathbf{else}:\\ \;\;\;\;-1 \cdot \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 -2.221067196710922123169723133116561516447 \cdot 10^{149}:\\
\;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\

\mathbf{elif}\;b \le 2.898348930695269343280527497904161468201 \cdot 10^{-35}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\sqrt{\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4}}, \sqrt{\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4}}, -b\right)}{2}}{a}\\

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

\end{array}
double f(double a, double b, double c) {
        double r5766635 = b;
        double r5766636 = -r5766635;
        double r5766637 = r5766635 * r5766635;
        double r5766638 = 4.0;
        double r5766639 = a;
        double r5766640 = r5766638 * r5766639;
        double r5766641 = c;
        double r5766642 = r5766640 * r5766641;
        double r5766643 = r5766637 - r5766642;
        double r5766644 = sqrt(r5766643);
        double r5766645 = r5766636 + r5766644;
        double r5766646 = 2.0;
        double r5766647 = r5766646 * r5766639;
        double r5766648 = r5766645 / r5766647;
        return r5766648;
}

double f(double a, double b, double c) {
        double r5766649 = b;
        double r5766650 = -2.221067196710922e+149;
        bool r5766651 = r5766649 <= r5766650;
        double r5766652 = c;
        double r5766653 = r5766652 / r5766649;
        double r5766654 = a;
        double r5766655 = r5766649 / r5766654;
        double r5766656 = r5766653 - r5766655;
        double r5766657 = 1.0;
        double r5766658 = r5766656 * r5766657;
        double r5766659 = 2.8983489306952693e-35;
        bool r5766660 = r5766649 <= r5766659;
        double r5766661 = r5766649 * r5766649;
        double r5766662 = r5766654 * r5766652;
        double r5766663 = 4.0;
        double r5766664 = r5766662 * r5766663;
        double r5766665 = r5766661 - r5766664;
        double r5766666 = sqrt(r5766665);
        double r5766667 = sqrt(r5766666);
        double r5766668 = -r5766649;
        double r5766669 = fma(r5766667, r5766667, r5766668);
        double r5766670 = 2.0;
        double r5766671 = r5766669 / r5766670;
        double r5766672 = r5766671 / r5766654;
        double r5766673 = -1.0;
        double r5766674 = r5766673 * r5766653;
        double r5766675 = r5766660 ? r5766672 : r5766674;
        double r5766676 = r5766651 ? r5766658 : r5766675;
        return r5766676;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.4
Target21.5
Herbie10.7
\[\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 3 regimes
  2. if b < -2.221067196710922e+149

    1. Initial program 62.3

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified62.3

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

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

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

    if -2.221067196710922e+149 < b < 2.8983489306952693e-35

    1. Initial program 14.6

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified14.6

      \[\leadsto \color{blue}{\frac{\frac{\sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)} - b}{2}}{a}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt14.6

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

      \[\leadsto \frac{\frac{\color{blue}{\sqrt{\sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}} \cdot \sqrt{\sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}} - b}{2}}{a}\]
    6. Applied fma-neg14.8

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

    if 2.8983489306952693e-35 < b

    1. Initial program 54.4

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified54.4

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -2.221067196710922123169723133116561516447 \cdot 10^{149}:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \mathbf{elif}\;b \le 2.898348930695269343280527497904161468201 \cdot 10^{-35}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\sqrt{\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4}}, \sqrt{\sqrt{b \cdot b - \left(a \cdot c\right) \cdot 4}}, -b\right)}{2}}{a}\\ \mathbf{else}:\\ \;\;\;\;-1 \cdot \frac{c}{b}\\ \end{array}\]

Reproduce

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