Average Error: 34.0 → 29.9
Time: 1.0m
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 4.1432508585428765 \cdot 10^{+166}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2} \cdot \frac{1}{a}\\ \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 4.1432508585428765 \cdot 10^{+166}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2} \cdot \frac{1}{a}\\

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

\end{array}
double f(double a, double b, double c) {
        double r31848017 = b;
        double r31848018 = -r31848017;
        double r31848019 = r31848017 * r31848017;
        double r31848020 = 4.0;
        double r31848021 = a;
        double r31848022 = r31848020 * r31848021;
        double r31848023 = c;
        double r31848024 = r31848022 * r31848023;
        double r31848025 = r31848019 - r31848024;
        double r31848026 = sqrt(r31848025);
        double r31848027 = r31848018 + r31848026;
        double r31848028 = 2.0;
        double r31848029 = r31848028 * r31848021;
        double r31848030 = r31848027 / r31848029;
        return r31848030;
}

double f(double a, double b, double c) {
        double r31848031 = b;
        double r31848032 = 4.1432508585428765e+166;
        bool r31848033 = r31848031 <= r31848032;
        double r31848034 = c;
        double r31848035 = -4.0;
        double r31848036 = a;
        double r31848037 = r31848035 * r31848036;
        double r31848038 = r31848031 * r31848031;
        double r31848039 = fma(r31848034, r31848037, r31848038);
        double r31848040 = sqrt(r31848039);
        double r31848041 = r31848040 - r31848031;
        double r31848042 = 2.0;
        double r31848043 = r31848041 / r31848042;
        double r31848044 = 1.0;
        double r31848045 = r31848044 / r31848036;
        double r31848046 = r31848043 * r31848045;
        double r31848047 = 0.0;
        double r31848048 = r31848033 ? r31848046 : r31848047;
        return r31848048;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.0
Target20.7
Herbie29.9
\[\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 < 4.1432508585428765e+166

    1. Initial program 28.3

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified28.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 div-inv28.4

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

    if 4.1432508585428765e+166 < b

    1. Initial program 62.9

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

      \[\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 div-inv62.9

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

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

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

Reproduce

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