Average Error: 34.3 → 6.8
Time: 10.5s
Precision: binary64
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
\[\begin{array}{l} \mathbf{if}\;b \leq -5.364212800122094 \cdot 10^{+119}:\\ \;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\ \mathbf{elif}\;b \leq -1.3269458115206028 \cdot 10^{-299}:\\ \;\;\;\;\frac{0}{2 \cdot a} - \frac{b - \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}}{2 \cdot a}\\ \mathbf{elif}\;b \leq 5.705473668258076 \cdot 10^{+56}:\\ \;\;\;\;\frac{c \cdot -2}{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \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 \leq -5.364212800122094 \cdot 10^{+119}:\\
\;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\

\mathbf{elif}\;b \leq -1.3269458115206028 \cdot 10^{-299}:\\
\;\;\;\;\frac{0}{2 \cdot a} - \frac{b - \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}}{2 \cdot a}\\

\mathbf{elif}\;b \leq 5.705473668258076 \cdot 10^{+56}:\\
\;\;\;\;\frac{c \cdot -2}{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}\\

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


\end{array}
(FPCore (a b c)
 :precision binary64
 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
(FPCore (a b c)
 :precision binary64
 (if (<= b -5.364212800122094e+119)
   (/ (- (- b) b) (* 2.0 a))
   (if (<= b -1.3269458115206028e-299)
     (-
      (/ 0.0 (* 2.0 a))
      (/ (- b (sqrt (- (* b b) (* (* a 4.0) c)))) (* 2.0 a)))
     (if (<= b 5.705473668258076e+56)
       (/ (* c -2.0) (+ b (sqrt (fma a (* c -4.0) (* b b)))))
       (- (/ c b))))))
double code(double a, double b, double c) {
	return (-b + sqrt((b * b) - ((4.0 * a) * c))) / (2.0 * a);
}
double code(double a, double b, double c) {
	double tmp;
	if (b <= -5.364212800122094e+119) {
		tmp = (-b - b) / (2.0 * a);
	} else if (b <= -1.3269458115206028e-299) {
		tmp = (0.0 / (2.0 * a)) - ((b - sqrt((b * b) - ((a * 4.0) * c))) / (2.0 * a));
	} else if (b <= 5.705473668258076e+56) {
		tmp = (c * -2.0) / (b + sqrt(fma(a, (c * -4.0), (b * b))));
	} else {
		tmp = -(c / b);
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.3
Target21.1
Herbie6.8
\[\begin{array}{l} \mathbf{if}\;b < 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 < -5.3642128001220939e119

    1. Initial program 52.4

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

      \[\leadsto \frac{\left(-b\right) + \color{blue}{-1 \cdot b}}{2 \cdot a} \]
    3. Simplified3.5

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

    if -5.3642128001220939e119 < b < -1.3269458115206028e-299

    1. Initial program 8.6

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

      \[\leadsto \frac{\color{blue}{\left(0 - b\right)} + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    3. Applied associate-+l-_binary648.6

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

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

    if -1.3269458115206028e-299 < b < 5.705473668258076e56

    1. Initial program 29.9

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

      \[\leadsto \color{blue}{\left(\sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)} - b\right) \cdot \frac{0.5}{a}} \]
    3. Applied flip--_binary6430.0

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

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

      \[\leadsto \frac{\color{blue}{\left(c \cdot \left(a \cdot -4\right) + 0\right) \cdot \frac{0.5}{a}}}{\sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)} + b} \]
    6. Taylor expanded in c around 0 9.7

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

    if 5.705473668258076e56 < b

    1. Initial program 57.6

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Simplified3.9

      \[\leadsto \color{blue}{-\frac{c}{b}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification6.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -5.364212800122094 \cdot 10^{+119}:\\ \;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\ \mathbf{elif}\;b \leq -1.3269458115206028 \cdot 10^{-299}:\\ \;\;\;\;\frac{0}{2 \cdot a} - \frac{b - \sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c}}{2 \cdot a}\\ \mathbf{elif}\;b \leq 5.705473668258076 \cdot 10^{+56}:\\ \;\;\;\;\frac{c \cdot -2}{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}\\ \mathbf{else}:\\ \;\;\;\;-\frac{c}{b}\\ \end{array} \]

Reproduce

herbie shell --seed 2021225 
(FPCore (a b c)
  :name "The quadratic formula (r1)"
  :precision binary64

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