Average Error: 34.6 → 7.0
Time: 18.4s
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 -3995085856351435358208:\\ \;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\ \mathbf{elif}\;b \le -3.157831273622663951144423397678142349188 \cdot 10^{-243}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt[3]{-b} \cdot \sqrt[3]{-b}, \sqrt[3]{-b}, \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}\right)}{a \cdot 2}\\ \mathbf{elif}\;b \le 1.721977985627455758133160718776844978069 \cdot 10^{83}:\\ \;\;\;\;\frac{4 \cdot c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(4 \cdot c\right) \cdot \left(-a\right)\right)}} \cdot \frac{1}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} \cdot -1\\ \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 -3995085856351435358208:\\
\;\;\;\;\left(\frac{c}{b} - \frac{b}{a}\right) \cdot 1\\

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

\mathbf{elif}\;b \le 1.721977985627455758133160718776844978069 \cdot 10^{83}:\\
\;\;\;\;\frac{4 \cdot c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(4 \cdot c\right) \cdot \left(-a\right)\right)}} \cdot \frac{1}{2}\\

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

\end{array}
double f(double a, double b, double c) {
        double r65221 = b;
        double r65222 = -r65221;
        double r65223 = r65221 * r65221;
        double r65224 = 4.0;
        double r65225 = a;
        double r65226 = r65224 * r65225;
        double r65227 = c;
        double r65228 = r65226 * r65227;
        double r65229 = r65223 - r65228;
        double r65230 = sqrt(r65229);
        double r65231 = r65222 + r65230;
        double r65232 = 2.0;
        double r65233 = r65232 * r65225;
        double r65234 = r65231 / r65233;
        return r65234;
}

double f(double a, double b, double c) {
        double r65235 = b;
        double r65236 = -3.9950858563514354e+21;
        bool r65237 = r65235 <= r65236;
        double r65238 = c;
        double r65239 = r65238 / r65235;
        double r65240 = a;
        double r65241 = r65235 / r65240;
        double r65242 = r65239 - r65241;
        double r65243 = 1.0;
        double r65244 = r65242 * r65243;
        double r65245 = -3.157831273622664e-243;
        bool r65246 = r65235 <= r65245;
        double r65247 = -r65235;
        double r65248 = cbrt(r65247);
        double r65249 = r65248 * r65248;
        double r65250 = r65235 * r65235;
        double r65251 = 4.0;
        double r65252 = r65240 * r65251;
        double r65253 = r65238 * r65252;
        double r65254 = r65250 - r65253;
        double r65255 = sqrt(r65254);
        double r65256 = fma(r65249, r65248, r65255);
        double r65257 = 2.0;
        double r65258 = r65240 * r65257;
        double r65259 = r65256 / r65258;
        double r65260 = 1.7219779856274558e+83;
        bool r65261 = r65235 <= r65260;
        double r65262 = r65251 * r65238;
        double r65263 = -r65240;
        double r65264 = r65262 * r65263;
        double r65265 = fma(r65235, r65235, r65264);
        double r65266 = sqrt(r65265);
        double r65267 = r65247 - r65266;
        double r65268 = r65262 / r65267;
        double r65269 = 1.0;
        double r65270 = r65269 / r65257;
        double r65271 = r65268 * r65270;
        double r65272 = -1.0;
        double r65273 = r65239 * r65272;
        double r65274 = r65261 ? r65271 : r65273;
        double r65275 = r65246 ? r65259 : r65274;
        double r65276 = r65237 ? r65244 : r65275;
        return r65276;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.6
Target21.0
Herbie7.0
\[\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 4 regimes
  2. if b < -3.9950858563514354e+21

    1. Initial program 35.4

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Taylor expanded around -inf 6.5

      \[\leadsto \color{blue}{1 \cdot \frac{c}{b} - 1 \cdot \frac{b}{a}}\]
    3. Simplified6.5

      \[\leadsto \color{blue}{1 \cdot \left(\frac{c}{b} - \frac{b}{a}\right)}\]

    if -3.9950858563514354e+21 < b < -3.157831273622664e-243

    1. Initial program 9.4

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied add-cube-cbrt9.7

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

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

    if -3.157831273622664e-243 < b < 1.7219779856274558e+83

    1. Initial program 29.5

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied flip-+29.6

      \[\leadsto \frac{\color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}}{2 \cdot a}\]
    4. Simplified16.0

      \[\leadsto \frac{\frac{\color{blue}{0 + \left(4 \cdot c\right) \cdot a}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}{2 \cdot a}\]
    5. Simplified16.0

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

      \[\leadsto \frac{\frac{0 + \left(4 \cdot c\right) \cdot a}{\color{blue}{1 \cdot \left(\left(-b\right) - \sqrt{\mathsf{fma}\left(a \cdot c, -4, b \cdot b\right)}\right)}}}{2 \cdot a}\]
    8. Applied *-un-lft-identity16.0

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

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

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

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

      \[\leadsto \frac{1}{2} \cdot \color{blue}{\frac{\frac{\left(4 \cdot c\right) \cdot a}{a}}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(-a\right) \cdot \left(4 \cdot c\right)\right)}}}\]
    13. Using strategy rm
    14. Applied associate-/l*9.3

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

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

    if 1.7219779856274558e+83 < 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. Taylor expanded around inf 2.5

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

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

Reproduce

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