Average Error: 18.0 → 5.9
Time: 10.5s
Precision: binary64
\[\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \end{array} \]
\[\begin{array}{l} t_0 := \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}\\ t_1 := \frac{2 \cdot c}{\left(-b\right) - t_0}\\ \mathbf{if}\;b \leq -4.402828950487531 \cdot 10^{+108}:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\ \end{array}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_2 := \sqrt{-b}\\ \mathbf{if}\;b \leq 1.7930757211015575 \cdot 10^{+105}:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(t_2, t_2, \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}\right)}{2 \cdot a}\\ \end{array}\\ \mathbf{elif}\;b \geq 0:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(t_2, t_2, t_0\right)}{2 \cdot a}\\ \end{array}\\ \end{array} \]
\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\

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


\end{array}
\begin{array}{l}
t_0 := \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}\\
t_1 := \frac{2 \cdot c}{\left(-b\right) - t_0}\\
\mathbf{if}\;b \leq -4.402828950487531 \cdot 10^{+108}:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\


\end{array}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_2 := \sqrt{-b}\\
\mathbf{if}\;b \leq 1.7930757211015575 \cdot 10^{+105}:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;t_1\\

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


\end{array}\\

\mathbf{elif}\;b \geq 0:\\
\;\;\;\;-\frac{c}{b}\\

\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(t_2, t_2, t_0\right)}{2 \cdot a}\\


\end{array}\\


\end{array}
(FPCore (a b c)
 :precision binary64
 (if (>= b 0.0)
   (/ (* 2.0 c) (- (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))))
   (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a))))
(FPCore (a b c)
 :precision binary64
 (let* ((t_0 (sqrt (- (* b b) (* c (* 4.0 a)))))
        (t_1 (/ (* 2.0 c) (- (- b) t_0))))
   (if (<= b -4.402828950487531e+108)
     (if (>= b 0.0) t_1 (/ (- (- b) b) (* 2.0 a)))
     (let* ((t_2 (sqrt (- b))))
       (if (<= b 1.7930757211015575e+105)
         (if (>= b 0.0)
           t_1
           (/ (fma t_2 t_2 (sqrt (fma b b (* c (* a -4.0))))) (* 2.0 a)))
         (if (>= b 0.0) (- (/ c b)) (/ (fma t_2 t_2 t_0) (* 2.0 a))))))))
double code(double a, double b, double c) {
	double tmp;
	if (b >= 0.0) {
		tmp = (2.0 * c) / (-b - sqrt((b * b) - ((4.0 * a) * c)));
	} else {
		tmp = (-b + sqrt((b * b) - ((4.0 * a) * c))) / (2.0 * a);
	}
	return tmp;
}
double code(double a, double b, double c) {
	double t_0 = sqrt((b * b) - (c * (4.0 * a)));
	double t_1 = (2.0 * c) / (-b - t_0);
	double tmp_1;
	if (b <= -4.402828950487531e+108) {
		double tmp_2;
		if (b >= 0.0) {
			tmp_2 = t_1;
		} else {
			tmp_2 = (-b - b) / (2.0 * a);
		}
		tmp_1 = tmp_2;
	} else {
		double t_2 = sqrt(-b);
		double tmp_4;
		if (b <= 1.7930757211015575e+105) {
			double tmp_5;
			if (b >= 0.0) {
				tmp_5 = t_1;
			} else {
				tmp_5 = fma(t_2, t_2, sqrt(fma(b, b, (c * (a * -4.0))))) / (2.0 * a);
			}
			tmp_4 = tmp_5;
		} else if (b >= 0.0) {
			tmp_4 = -(c / b);
		} else {
			tmp_4 = fma(t_2, t_2, t_0) / (2.0 * a);
		}
		tmp_1 = tmp_4;
	}
	return tmp_1;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Derivation

  1. Split input into 3 regimes
  2. if b < -4.40282895048753084e108

    1. Initial program 30.4

      \[\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \end{array} \]
    2. Taylor expanded in b around -inf 2.1

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) + -1 \cdot b}{2 \cdot a}\\ \end{array} \]

    if -4.40282895048753084e108 < b < 1.7930757211015575e105

    1. Initial program 8.7

      \[\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \end{array} \]
    2. Applied add-sqr-sqrt_binary648.7

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{-b} \cdot \sqrt{-b} + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \end{array} \]
    3. Applied fma-def_binary648.7

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt{-b}, \sqrt{-b}, \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}\\ \end{array} \]
    4. Taylor expanded in b around 0 8.7

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt{-b}, \sqrt{-b}, \sqrt{{b}^{2} - 4 \cdot \left(c \cdot a\right)}\right)}{2 \cdot a}\\ \end{array} \]
    5. Simplified8.7

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

    if 1.7930757211015575e105 < b

    1. Initial program 30.3

      \[\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \end{array} \]
    2. Applied add-sqr-sqrt_binary6430.3

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\sqrt{-b} \cdot \sqrt{-b} + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \end{array} \]
    3. Applied fma-def_binary6430.3

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt{-b}, \sqrt{-b}, \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}\\ \end{array} \]
    4. Taylor expanded in c around 0 2.3

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\color{blue}{-1 \cdot \frac{c}{b}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt{-b}, \sqrt{-b}, \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}\\ \end{array} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification5.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -4.402828950487531 \cdot 10^{+108}:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - b}{2 \cdot a}\\ \end{array}\\ \mathbf{elif}\;b \leq 1.7930757211015575 \cdot 10^{+105}:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) - \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt{-b}, \sqrt{-b}, \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}\right)}{2 \cdot a}\\ \end{array}\\ \mathbf{elif}\;b \geq 0:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\sqrt{-b}, \sqrt{-b}, \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}\right)}{2 \cdot a}\\ \end{array} \]

Reproduce

herbie shell --seed 2022088 
(FPCore (a b c)
  :name "jeff quadratic root 2"
  :precision binary64
  (if (>= b 0.0) (/ (* 2.0 c) (- (- b) (sqrt (- (* b b) (* (* 4.0 a) c))))) (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a))))