Average Error: 32.9 → 28.7
Time: 1.1m
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 7.844448680425584 \cdot 10^{+101}:\\ \;\;\;\;\frac{\frac{1}{2}}{a} \cdot \left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \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 7.844448680425584 \cdot 10^{+101}:\\
\;\;\;\;\frac{\frac{1}{2}}{a} \cdot \left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right)\\

\mathbf{else}:\\
\;\;\;\;0\\

\end{array}
double f(double a, double b, double c) {
        double r5557607 = b;
        double r5557608 = -r5557607;
        double r5557609 = r5557607 * r5557607;
        double r5557610 = 4.0;
        double r5557611 = a;
        double r5557612 = r5557610 * r5557611;
        double r5557613 = c;
        double r5557614 = r5557612 * r5557613;
        double r5557615 = r5557609 - r5557614;
        double r5557616 = sqrt(r5557615);
        double r5557617 = r5557608 + r5557616;
        double r5557618 = 2.0;
        double r5557619 = r5557618 * r5557611;
        double r5557620 = r5557617 / r5557619;
        return r5557620;
}

double f(double a, double b, double c) {
        double r5557621 = b;
        double r5557622 = 7.844448680425584e+101;
        bool r5557623 = r5557621 <= r5557622;
        double r5557624 = 0.5;
        double r5557625 = a;
        double r5557626 = r5557624 / r5557625;
        double r5557627 = c;
        double r5557628 = -4.0;
        double r5557629 = r5557625 * r5557628;
        double r5557630 = r5557621 * r5557621;
        double r5557631 = fma(r5557627, r5557629, r5557630);
        double r5557632 = sqrt(r5557631);
        double r5557633 = r5557632 - r5557621;
        double r5557634 = r5557626 * r5557633;
        double r5557635 = 0.0;
        double r5557636 = r5557623 ? r5557634 : r5557635;
        return r5557636;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original32.9
Target20.2
Herbie28.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 2 regimes
  2. if b < 7.844448680425584e+101

    1. Initial program 25.3

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

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

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

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

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

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

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

    if 7.844448680425584e+101 < b

    1. Initial program 59.0

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

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

      \[\leadsto \color{blue}{0}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification28.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le 7.844448680425584 \cdot 10^{+101}:\\ \;\;\;\;\frac{\frac{1}{2}}{a} \cdot \left(\sqrt{\mathsf{fma}\left(c, \left(a \cdot -4\right), \left(b \cdot b\right)\right)} - b\right)\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array}\]

Reproduce

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