Average Error: 34.5 → 10.4
Time: 15.8s
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 r4877236 = b;
        double r4877237 = -r4877236;
        double r4877238 = r4877236 * r4877236;
        double r4877239 = 4.0;
        double r4877240 = a;
        double r4877241 = r4877239 * r4877240;
        double r4877242 = c;
        double r4877243 = r4877241 * r4877242;
        double r4877244 = r4877238 - r4877243;
        double r4877245 = sqrt(r4877244);
        double r4877246 = r4877237 + r4877245;
        double r4877247 = 2.0;
        double r4877248 = r4877247 * r4877240;
        double r4877249 = r4877246 / r4877248;
        return r4877249;
}

double f(double a, double b, double c) {
        double r4877250 = b;
        double r4877251 = -6.336287344206649e+28;
        bool r4877252 = r4877250 <= r4877251;
        double r4877253 = a;
        double r4877254 = r4877250 / r4877253;
        double r4877255 = -2.0;
        double r4877256 = 2.0;
        double r4877257 = c;
        double r4877258 = r4877257 / r4877250;
        double r4877259 = r4877256 * r4877258;
        double r4877260 = fma(r4877254, r4877255, r4877259);
        double r4877261 = r4877260 / r4877256;
        double r4877262 = 6.484072051994264e-107;
        bool r4877263 = r4877250 <= r4877262;
        double r4877264 = r4877250 * r4877250;
        double r4877265 = 4.0;
        double r4877266 = r4877265 * r4877257;
        double r4877267 = r4877253 * r4877266;
        double r4877268 = r4877264 - r4877267;
        double r4877269 = cbrt(r4877268);
        double r4877270 = r4877269 * r4877269;
        double r4877271 = sqrt(r4877270);
        double r4877272 = sqrt(r4877269);
        double r4877273 = -r4877250;
        double r4877274 = fma(r4877271, r4877272, r4877273);
        double r4877275 = r4877274 / r4877253;
        double r4877276 = r4877275 / r4877256;
        double r4877277 = -2.0;
        double r4877278 = r4877258 * r4877277;
        double r4877279 = r4877278 / r4877256;
        double r4877280 = r4877263 ? r4877276 : r4877279;
        double r4877281 = r4877252 ? r4877261 : r4877280;
        return r4877281;
}

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)))