Average Error: 34.0 → 10.1
Time: 8.1s
Precision: binary64
\[\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
\[\begin{array}{l} t_0 := c \cdot \left(a \cdot -4\right)\\ t_1 := \frac{-c}{b}\\ \mathbf{if}\;b \leq -2.15 \cdot 10^{+105}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 6.2 \cdot 10^{-55}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, t_0\right)} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 1.7 \cdot 10^{-46}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;b \leq 6.6 \cdot 10^{-22}:\\ \;\;\;\;\frac{1}{a \cdot \frac{2}{\mathsf{hypot}\left(b, {t_0}^{0.5}\right) - b}}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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
 (let* ((t_0 (* c (* a -4.0))) (t_1 (/ (- c) b)))
   (if (<= b -2.15e+105)
     (- (/ c b) (/ b a))
     (if (<= b 6.2e-55)
       (/ (- (sqrt (fma b b t_0)) b) (* a 2.0))
       (if (<= b 1.7e-46)
         t_1
         (if (<= b 6.6e-22)
           (/ 1.0 (* a (/ 2.0 (- (hypot b (pow t_0 0.5)) b))))
           t_1))))))
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 t_0 = c * (a * -4.0);
	double t_1 = -c / b;
	double tmp;
	if (b <= -2.15e+105) {
		tmp = (c / b) - (b / a);
	} else if (b <= 6.2e-55) {
		tmp = (sqrt(fma(b, b, t_0)) - b) / (a * 2.0);
	} else if (b <= 1.7e-46) {
		tmp = t_1;
	} else if (b <= 6.6e-22) {
		tmp = 1.0 / (a * (2.0 / (hypot(b, pow(t_0, 0.5)) - b)));
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(a, b, c)
	return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * a))
end
function code(a, b, c)
	t_0 = Float64(c * Float64(a * -4.0))
	t_1 = Float64(Float64(-c) / b)
	tmp = 0.0
	if (b <= -2.15e+105)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	elseif (b <= 6.2e-55)
		tmp = Float64(Float64(sqrt(fma(b, b, t_0)) - b) / Float64(a * 2.0));
	elseif (b <= 1.7e-46)
		tmp = t_1;
	elseif (b <= 6.6e-22)
		tmp = Float64(1.0 / Float64(a * Float64(2.0 / Float64(hypot(b, (t_0 ^ 0.5)) - b))));
	else
		tmp = t_1;
	end
	return tmp
end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[((-c) / b), $MachinePrecision]}, If[LessEqual[b, -2.15e+105], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 6.2e-55], N[(N[(N[Sqrt[N[(b * b + t$95$0), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.7e-46], t$95$1, If[LessEqual[b, 6.6e-22], N[(1.0 / N[(a * N[(2.0 / N[(N[Sqrt[b ^ 2 + N[Power[t$95$0, 0.5], $MachinePrecision] ^ 2], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
t_0 := c \cdot \left(a \cdot -4\right)\\
t_1 := \frac{-c}{b}\\
\mathbf{if}\;b \leq -2.15 \cdot 10^{+105}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \leq 6.2 \cdot 10^{-55}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, t_0\right)} - b}{a \cdot 2}\\

\mathbf{elif}\;b \leq 1.7 \cdot 10^{-46}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;b \leq 6.6 \cdot 10^{-22}:\\
\;\;\;\;\frac{1}{a \cdot \frac{2}{\mathsf{hypot}\left(b, {t_0}^{0.5}\right) - b}}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.0
Target20.8
Herbie10.1
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}\\ \end{array} \]

Derivation

  1. Split input into 4 regimes
  2. if b < -2.1500000000000001e105

    1. Initial program 47.0

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

      \[\leadsto \color{blue}{\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)} - b}{a \cdot 2}} \]
    3. Taylor expanded in b around -inf 3.0

      \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]

    if -2.1500000000000001e105 < b < 6.19999999999999993e-55

    1. Initial program 13.5

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

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

    if 6.19999999999999993e-55 < b < 1.69999999999999998e-46 or 6.6000000000000002e-22 < b

    1. Initial program 54.5

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

      \[\leadsto \color{blue}{\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)} - b}{a \cdot 2}} \]
    3. Taylor expanded in b around inf 7.2

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    4. Simplified7.2

      \[\leadsto \color{blue}{\frac{-c}{b}} \]

    if 1.69999999999999998e-46 < b < 6.6000000000000002e-22

    1. Initial program 37.2

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

      \[\leadsto \color{blue}{\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)} - b}{a \cdot 2}} \]
    3. Applied egg-rr38.5

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

      \[\leadsto \color{blue}{\frac{1}{a \cdot \frac{2}{\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b}}} \]
    5. Applied egg-rr38.6

      \[\leadsto \frac{1}{a \cdot \frac{2}{\mathsf{hypot}\left(b, \color{blue}{{\left(c \cdot \left(a \cdot -4\right)\right)}^{0.5}}\right) - b}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification10.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -2.15 \cdot 10^{+105}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 6.2 \cdot 10^{-55}:\\ \;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)} - b}{a \cdot 2}\\ \mathbf{elif}\;b \leq 1.7 \cdot 10^{-46}:\\ \;\;\;\;\frac{-c}{b}\\ \mathbf{elif}\;b \leq 6.6 \cdot 10^{-22}:\\ \;\;\;\;\frac{1}{a \cdot \frac{2}{\mathsf{hypot}\left(b, {\left(c \cdot \left(a \cdot -4\right)\right)}^{0.5}\right) - b}}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Reproduce

herbie shell --seed 2022155 
(FPCore (a b c)
  :name "quadp (p42, positive)"
  :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)))