Average Error: 34.5 → 10.4
Time: 16.1s
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 -63362873442066488610789523456:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{a}, -2, 2 \cdot \frac{c}{b}\right)}{2}\\ \mathbf{elif}\;b \le 6.484072051994263737451444554171174935457 \cdot 10^{-107}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\sqrt{\sqrt[3]{b \cdot b - a \cdot \left(4 \cdot c\right)} \cdot \sqrt[3]{b \cdot b - a \cdot \left(4 \cdot c\right)}}, \sqrt{\sqrt[3]{b \cdot b - a \cdot \left(4 \cdot c\right)}}, -b\right)}{a}}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{b} \cdot -2}{2}\\ \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 -63362873442066488610789523456:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{a}, -2, 2 \cdot \frac{c}{b}\right)}{2}\\

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

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

\end{array}
double f(double a, double b, double c) {
        double r2867292 = b;
        double r2867293 = -r2867292;
        double r2867294 = r2867292 * r2867292;
        double r2867295 = 4.0;
        double r2867296 = a;
        double r2867297 = r2867295 * r2867296;
        double r2867298 = c;
        double r2867299 = r2867297 * r2867298;
        double r2867300 = r2867294 - r2867299;
        double r2867301 = sqrt(r2867300);
        double r2867302 = r2867293 + r2867301;
        double r2867303 = 2.0;
        double r2867304 = r2867303 * r2867296;
        double r2867305 = r2867302 / r2867304;
        return r2867305;
}

double f(double a, double b, double c) {
        double r2867306 = b;
        double r2867307 = -6.336287344206649e+28;
        bool r2867308 = r2867306 <= r2867307;
        double r2867309 = a;
        double r2867310 = r2867306 / r2867309;
        double r2867311 = -2.0;
        double r2867312 = 2.0;
        double r2867313 = c;
        double r2867314 = r2867313 / r2867306;
        double r2867315 = r2867312 * r2867314;
        double r2867316 = fma(r2867310, r2867311, r2867315);
        double r2867317 = r2867316 / r2867312;
        double r2867318 = 6.484072051994264e-107;
        bool r2867319 = r2867306 <= r2867318;
        double r2867320 = r2867306 * r2867306;
        double r2867321 = 4.0;
        double r2867322 = r2867321 * r2867313;
        double r2867323 = r2867309 * r2867322;
        double r2867324 = r2867320 - r2867323;
        double r2867325 = cbrt(r2867324);
        double r2867326 = r2867325 * r2867325;
        double r2867327 = sqrt(r2867326);
        double r2867328 = sqrt(r2867325);
        double r2867329 = -r2867306;
        double r2867330 = fma(r2867327, r2867328, r2867329);
        double r2867331 = r2867330 / r2867309;
        double r2867332 = r2867331 / r2867312;
        double r2867333 = -2.0;
        double r2867334 = r2867314 * r2867333;
        double r2867335 = r2867334 / r2867312;
        double r2867336 = r2867319 ? r2867332 : r2867335;
        double r2867337 = r2867308 ? r2867317 : r2867336;
        return r2867337;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.5
Target21.0
Herbie10.4
\[\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 < -6.336287344206649e+28

    1. Initial program 34.8

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

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

      \[\leadsto \frac{\color{blue}{2 \cdot \frac{c}{b} - 2 \cdot \frac{b}{a}}}{2}\]
    4. Simplified7.0

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

    if -6.336287344206649e+28 < b < 6.484072051994264e-107

    1. Initial program 12.9

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

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

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

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

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

    if 6.484072051994264e-107 < b

    1. Initial program 52.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -63362873442066488610789523456:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{a}, -2, 2 \cdot \frac{c}{b}\right)}{2}\\ \mathbf{elif}\;b \le 6.484072051994263737451444554171174935457 \cdot 10^{-107}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(\sqrt{\sqrt[3]{b \cdot b - a \cdot \left(4 \cdot c\right)} \cdot \sqrt[3]{b \cdot b - a \cdot \left(4 \cdot c\right)}}, \sqrt{\sqrt[3]{b \cdot b - a \cdot \left(4 \cdot c\right)}}, -b\right)}{a}}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{b} \cdot -2}{2}\\ \end{array}\]

Reproduce

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