Average Error: 19.5 → 6.3
Time: 10.9s
Precision: binary64
\[\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{2 \cdot c}{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\\ \end{array} \]
\[\begin{array}{l} t_0 := \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\\ t_1 := \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - t_0}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{t_0 - b}\\ \end{array}\\ t_2 := \frac{c \cdot 2}{\left(-b\right) - b}\\ t_3 := \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{b + b}{a}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;t_3\\ \mathbf{elif}\;t_1 \leq -9.915702647006648 \cdot 10^{-258}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_1 \leq 0:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\begin{array}{l} t_4 := \sqrt{b + \mathsf{hypot}\left(\sqrt{c \cdot \left(a \cdot -4\right)}, b\right)}\\ -0.5 \cdot \left(t_4 \cdot \frac{t_4}{a}\right) \end{array}\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array}\\ \mathbf{elif}\;t_1 \leq 5.305809263431213 \cdot 10^{+253}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_3\\ \end{array} \]
\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\

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


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

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


\end{array}\\
t_2 := \frac{c \cdot 2}{\left(-b\right) - b}\\
t_3 := \begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;-0.5 \cdot \frac{b + b}{a}\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;t_3\\

\mathbf{elif}\;t_1 \leq -9.915702647006648 \cdot 10^{-258}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_1 \leq 0:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\begin{array}{l}
t_4 := \sqrt{b + \mathsf{hypot}\left(\sqrt{c \cdot \left(a \cdot -4\right)}, b\right)}\\
-0.5 \cdot \left(t_4 \cdot \frac{t_4}{a}\right)
\end{array}\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}\\

\mathbf{elif}\;t_1 \leq 5.305809263431213 \cdot 10^{+253}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_3\\


\end{array}
(FPCore (a b c)
 :precision binary64
 (if (>= b 0.0)
   (/ (- (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a))
   (/ (* 2.0 c) (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))))))
(FPCore (a b c)
 :precision binary64
 (let* ((t_0 (sqrt (- (* b b) (* (* 4.0 a) c))))
        (t_1
         (if (>= b 0.0) (/ (- (- b) t_0) (* a 2.0)) (/ (* c 2.0) (- t_0 b))))
        (t_2 (/ (* c 2.0) (- (- b) b)))
        (t_3 (if (>= b 0.0) (* -0.5 (/ (+ b b) a)) t_2)))
   (if (<= t_1 (- INFINITY))
     t_3
     (if (<= t_1 -9.915702647006648e-258)
       t_1
       (if (<= t_1 0.0)
         (if (>= b 0.0)
           (let* ((t_4 (sqrt (+ b (hypot (sqrt (* c (* a -4.0))) b)))))
             (* -0.5 (* t_4 (/ t_4 a))))
           t_2)
         (if (<= t_1 5.305809263431213e+253) t_1 t_3))))))
double code(double a, double b, double c) {
	double tmp;
	if (b >= 0.0) {
		tmp = (-b - sqrt((b * b) - ((4.0 * a) * c))) / (2.0 * a);
	} else {
		tmp = (2.0 * c) / (-b + sqrt((b * b) - ((4.0 * a) * c)));
	}
	return tmp;
}
double code(double a, double b, double c) {
	double t_0 = sqrt((b * b) - ((4.0 * a) * c));
	double tmp;
	if (b >= 0.0) {
		tmp = (-b - t_0) / (a * 2.0);
	} else {
		tmp = (c * 2.0) / (t_0 - b);
	}
	double t_1 = tmp;
	double t_2 = (c * 2.0) / (-b - b);
	double tmp_1;
	if (b >= 0.0) {
		tmp_1 = -0.5 * ((b + b) / a);
	} else {
		tmp_1 = t_2;
	}
	double t_3 = tmp_1;
	double tmp_2;
	if (t_1 <= -((double) INFINITY)) {
		tmp_2 = t_3;
	} else if (t_1 <= -9.915702647006648e-258) {
		tmp_2 = t_1;
	} else if (t_1 <= 0.0) {
		double tmp_3;
		if (b >= 0.0) {
			double t_4_4 = sqrt(b + hypot(sqrt(c * (a * -4.0)), b));
			tmp_3 = -0.5 * (t_4_4 * (t_4_4 / a));
		} else {
			tmp_3 = t_2;
		}
		tmp_2 = tmp_3;
	} else if (t_1 <= 5.305809263431213e+253) {
		tmp_2 = t_1;
	} else {
		tmp_2 = t_3;
	}
	return tmp_2;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 3 regimes
  2. if (if (>=.f64 b 0) (/.f64 (-.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) (/.f64 (*.f64 2 c) (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))))) < -inf.0 or 5.3058092634312132e253 < (if (>=.f64 b 0) (/.f64 (-.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) (/.f64 (*.f64 2 c) (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c))))))

    1. Initial program 59.7

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

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

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{b + \color{blue}{b}}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{-1 \cdot b - b}\\ \end{array} \]

    if -inf.0 < (if (>=.f64 b 0) (/.f64 (-.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) (/.f64 (*.f64 2 c) (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))))) < -9.91570264700664773e-258 or -0.0 < (if (>=.f64 b 0) (/.f64 (-.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) (/.f64 (*.f64 2 c) (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))))) < 5.3058092634312132e253

    1. Initial program 2.6

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

    if -9.91570264700664773e-258 < (if (>=.f64 b 0) (/.f64 (-.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) (/.f64 (*.f64 2 c) (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))))) < -0.0

    1. Initial program 36.2

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

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{\color{blue}{c \cdot 2}}{-1 \cdot b - b}\\ \end{array} \]
    4. Applied *-un-lft-identity_binary6410.0

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}{\color{blue}{1 \cdot a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{-1 \cdot b - b}\\ \end{array} \]
    5. Applied add-sqr-sqrt_binary6410.0

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{\color{blue}{\sqrt{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}} \cdot \sqrt{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}}}{1 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{-1 \cdot b - b}\\ \end{array} \]
    6. Applied times-frac_binary6410.0

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \left(\color{blue}{\sqrt{b + \mathsf{hypot}\left(\sqrt{c \cdot \left(a \cdot -4\right)}, b\right)}} \cdot \frac{\sqrt{b + \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}}}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{-1 \cdot b - b}\\ \end{array} \]
    8. Simplified10.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}\\ \end{array} \leq -\infty:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{b + b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - b}\\ \end{array}\\ \mathbf{elif}\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}\\ \end{array} \leq -9.915702647006648 \cdot 10^{-258}:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}\\ \end{array}\\ \mathbf{elif}\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}\\ \end{array} \leq 0:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \left(\sqrt{b + \mathsf{hypot}\left(\sqrt{c \cdot \left(a \cdot -4\right)}, b\right)} \cdot \frac{\sqrt{b + \mathsf{hypot}\left(\sqrt{c \cdot \left(a \cdot -4\right)}, b\right)}}{a}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - b}\\ \end{array}\\ \mathbf{elif}\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}\\ \end{array} \leq 5.305809263431213 \cdot 10^{+253}:\\ \;\;\;\;\begin{array}{l} \mathbf{if}\;b \geq 0:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}\\ \end{array}\\ \mathbf{elif}\;b \geq 0:\\ \;\;\;\;-0.5 \cdot \frac{b + b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - b}\\ \end{array} \]

Reproduce

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