Average Error: 32.9 → 28.7
Time: 1.5m
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 r3528381 = b;
        double r3528382 = -r3528381;
        double r3528383 = r3528381 * r3528381;
        double r3528384 = 4.0;
        double r3528385 = a;
        double r3528386 = r3528384 * r3528385;
        double r3528387 = c;
        double r3528388 = r3528386 * r3528387;
        double r3528389 = r3528383 - r3528388;
        double r3528390 = sqrt(r3528389);
        double r3528391 = r3528382 + r3528390;
        double r3528392 = 2.0;
        double r3528393 = r3528392 * r3528385;
        double r3528394 = r3528391 / r3528393;
        return r3528394;
}

double f(double a, double b, double c) {
        double r3528395 = b;
        double r3528396 = 7.844448680425584e+101;
        bool r3528397 = r3528395 <= r3528396;
        double r3528398 = 0.5;
        double r3528399 = a;
        double r3528400 = r3528398 / r3528399;
        double r3528401 = c;
        double r3528402 = -4.0;
        double r3528403 = r3528399 * r3528402;
        double r3528404 = r3528395 * r3528395;
        double r3528405 = fma(r3528401, r3528403, r3528404);
        double r3528406 = sqrt(r3528405);
        double r3528407 = r3528406 - r3528395;
        double r3528408 = r3528400 * r3528407;
        double r3528409 = 0.0;
        double r3528410 = r3528397 ? r3528408 : r3528409;
        return r3528410;
}

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