Average Error: 32.9 → 22.1
Time: 2.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 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 r21822708 = b;
        double r21822709 = -r21822708;
        double r21822710 = r21822708 * r21822708;
        double r21822711 = 4.0;
        double r21822712 = a;
        double r21822713 = r21822711 * r21822712;
        double r21822714 = c;
        double r21822715 = r21822713 * r21822714;
        double r21822716 = r21822710 - r21822715;
        double r21822717 = sqrt(r21822716);
        double r21822718 = r21822709 + r21822717;
        double r21822719 = 2.0;
        double r21822720 = r21822719 * r21822712;
        double r21822721 = r21822718 / r21822720;
        return r21822721;
}

double f(double a, double b, double c) {
        double r21822722 = b;
        double r21822723 = 2.399747870429879e-86;
        bool r21822724 = r21822722 <= r21822723;
        double r21822725 = c;
        double r21822726 = -4.0;
        double r21822727 = r21822725 * r21822726;
        double r21822728 = a;
        double r21822729 = r21822722 * r21822722;
        double r21822730 = fma(r21822727, r21822728, r21822729);
        double r21822731 = sqrt(r21822730);
        double r21822732 = r21822731 - r21822722;
        double r21822733 = r21822732 / r21822728;
        double r21822734 = 0.5;
        double r21822735 = r21822733 * r21822734;
        double r21822736 = r21822727 * r21822728;
        double r21822737 = r21822722 + r21822731;
        double r21822738 = r21822737 * r21822728;
        double r21822739 = r21822736 / r21822738;
        double r21822740 = r21822734 * r21822739;
        double r21822741 = r21822724 ? r21822735 : r21822740;
        return r21822741;
}

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{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - \color{blue}{1 \cdot b}}{1 \cdot 2}}{a}\]
    6. Applied *-un-lft-identity20.0

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)}} - 1 \cdot b}{1 \cdot 2}}{a}\]
    7. Applied distribute-lft-out--20.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}\]
    8. 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}\]
    9. 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}}}}\]
    10. 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}}}\]
    11. Using strategy rm
    12. 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}}\]
    13. Applied add-sqr-sqrt20.1

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

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\frac{a}{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}} \cdot \frac{\sqrt{1}}{2}}\]
    15. 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{1}}{2}\]
    16. 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{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - \color{blue}{1 \cdot b}}{1 \cdot 2}}{a}\]
    6. Applied *-un-lft-identity52.2

      \[\leadsto \frac{\frac{\color{blue}{1 \cdot \sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)}} - 1 \cdot b}{1 \cdot 2}}{a}\]
    7. Applied distribute-lft-out--52.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}\]
    8. 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}\]
    9. 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}}}}\]
    10. 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}}}\]
    11. Using strategy rm
    12. 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}}\]
    13. Applied add-sqr-sqrt52.2

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

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\frac{a}{\sqrt{\mathsf{fma}\left(c, \left(-4 \cdot a\right), \left(b \cdot b\right)\right)} - b}} \cdot \frac{\sqrt{1}}{2}}\]
    15. 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{1}}{2}\]
    16. 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}}\]
    17. Using strategy rm
    18. 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}\]
    19. 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}\]
    20. 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)))