Average Error: 32.9 → 22.1
Time: 2.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 2.399747870429879 \cdot 10^{-86}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} - b}{a} \cdot \frac{1}{2}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{2} \cdot \frac{\left(c \cdot -4\right) \cdot a}{\left(b + \sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)}\right) \cdot a}\\ \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 2.399747870429879 \cdot 10^{-86}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} - b}{a} \cdot \frac{1}{2}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{2} \cdot \frac{\left(c \cdot -4\right) \cdot a}{\left(b + \sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)}\right) \cdot a}\\

\end{array}
double f(double a, double b, double c) {
        double r14663175 = b;
        double r14663176 = -r14663175;
        double r14663177 = r14663175 * r14663175;
        double r14663178 = 4.0;
        double r14663179 = a;
        double r14663180 = r14663178 * r14663179;
        double r14663181 = c;
        double r14663182 = r14663180 * r14663181;
        double r14663183 = r14663177 - r14663182;
        double r14663184 = sqrt(r14663183);
        double r14663185 = r14663176 + r14663184;
        double r14663186 = 2.0;
        double r14663187 = r14663186 * r14663179;
        double r14663188 = r14663185 / r14663187;
        return r14663188;
}

double f(double a, double b, double c) {
        double r14663189 = b;
        double r14663190 = 2.399747870429879e-86;
        bool r14663191 = r14663189 <= r14663190;
        double r14663192 = c;
        double r14663193 = -4.0;
        double r14663194 = r14663192 * r14663193;
        double r14663195 = a;
        double r14663196 = r14663189 * r14663189;
        double r14663197 = fma(r14663194, r14663195, r14663196);
        double r14663198 = sqrt(r14663197);
        double r14663199 = r14663198 - r14663189;
        double r14663200 = r14663199 / r14663195;
        double r14663201 = 0.5;
        double r14663202 = r14663200 * r14663201;
        double r14663203 = r14663194 * r14663195;
        double r14663204 = r14663189 + r14663198;
        double r14663205 = r14663204 * r14663195;
        double r14663206 = r14663203 / r14663205;
        double r14663207 = r14663201 * r14663206;
        double r14663208 = r14663191 ? r14663202 : r14663207;
        return r14663208;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original32.9
Target20.3
Herbie22.1
\[\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 < 2.399747870429879e-86

    1. Initial program 20.0

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Simplified20.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. Using strategy rm
    4. Applied *-un-lft-identity20.0

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

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

      \[\leadsto \frac{\color{blue}{\frac{1}{1} \cdot \frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2}}}{a}\]
    7. Applied associate-/l*20.1

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

      \[\leadsto \frac{\color{blue}{1}}{\frac{a}{\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2}}}\]
    9. Using strategy rm
    10. Applied associate-/r/20.1

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

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

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

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

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

    if 2.399747870429879e-86 < b

    1. Initial program 52.1

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

      \[\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-identity52.2

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

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

      \[\leadsto \frac{\color{blue}{\frac{1}{1} \cdot \frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2}}}{a}\]
    7. Applied associate-/l*52.2

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

      \[\leadsto \frac{\color{blue}{1}}{\frac{a}{\frac{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}{2}}}\]
    9. Using strategy rm
    10. Applied associate-/r/52.2

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

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

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

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

      \[\leadsto \frac{\sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} - b}{a} \cdot \color{blue}{\frac{1}{2}}\]
    15. Using strategy rm
    16. Applied flip--52.2

      \[\leadsto \frac{\color{blue}{\frac{\sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} \cdot \sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} - b \cdot b}{\sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} + b}}}{a} \cdot \frac{1}{2}\]
    17. Applied associate-/l/53.4

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

      \[\leadsto \frac{\color{blue}{\left(c \cdot -4\right) \cdot a}}{a \cdot \left(\sqrt{\mathsf{fma}\left(\left(c \cdot -4\right), a, \left(b \cdot b\right)\right)} + b\right)} \cdot \frac{1}{2}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification22.1

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

Reproduce

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