Average Error: 34.4 → 14.6
Time: 13.4s
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 := -\frac{c}{b}\\ t_1 := \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2}\\ t_2 := \frac{\mathsf{fma}\left(-1, b, \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)}\right)}{a \cdot 2}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq -7.311089820331434 \cdot 10^{-265}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 0:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq 5.78568889785705 \cdot 10^{+298}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(b, -1, \mathsf{hypot}\left(b, \sqrt{a} \cdot \sqrt{c \cdot -4}\right)\right) \cdot \frac{0.5}{a}\\ \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 b)))
        (t_1 (/ (- (sqrt (- (* b b) (* 4.0 (* a c)))) b) (* a 2.0)))
        (t_2 (/ (fma -1.0 b (sqrt (fma b b (* (* a c) -4.0)))) (* a 2.0))))
   (if (<= t_1 (- INFINITY))
     t_0
     (if (<= t_1 -7.311089820331434e-265)
       t_2
       (if (<= t_1 0.0)
         t_0
         (if (<= t_1 5.78568889785705e+298)
           t_2
           (*
            (fma b -1.0 (hypot b (* (sqrt a) (sqrt (* c -4.0)))))
            (/ 0.5 a))))))))
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 / b);
	double t_1 = (sqrt(((b * b) - (4.0 * (a * c)))) - b) / (a * 2.0);
	double t_2 = fma(-1.0, b, sqrt(fma(b, b, ((a * c) * -4.0)))) / (a * 2.0);
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = t_0;
	} else if (t_1 <= -7.311089820331434e-265) {
		tmp = t_2;
	} else if (t_1 <= 0.0) {
		tmp = t_0;
	} else if (t_1 <= 5.78568889785705e+298) {
		tmp = t_2;
	} else {
		tmp = fma(b, -1.0, hypot(b, (sqrt(a) * sqrt((c * -4.0))))) * (0.5 / a);
	}
	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(-Float64(c / b))
	t_1 = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c)))) - b) / Float64(a * 2.0))
	t_2 = Float64(fma(-1.0, b, sqrt(fma(b, b, Float64(Float64(a * c) * -4.0)))) / Float64(a * 2.0))
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = t_0;
	elseif (t_1 <= -7.311089820331434e-265)
		tmp = t_2;
	elseif (t_1 <= 0.0)
		tmp = t_0;
	elseif (t_1 <= 5.78568889785705e+298)
		tmp = t_2;
	else
		tmp = Float64(fma(b, -1.0, hypot(b, Float64(sqrt(a) * sqrt(Float64(c * -4.0))))) * Float64(0.5 / a));
	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 / b), $MachinePrecision])}, Block[{t$95$1 = N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(-1.0 * b + N[Sqrt[N[(b * b + N[(N[(a * c), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], t$95$0, If[LessEqual[t$95$1, -7.311089820331434e-265], t$95$2, If[LessEqual[t$95$1, 0.0], t$95$0, If[LessEqual[t$95$1, 5.78568889785705e+298], t$95$2, N[(N[(b * -1.0 + N[Sqrt[b ^ 2 + N[(N[Sqrt[a], $MachinePrecision] * N[Sqrt[N[(c * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision]]]]]]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
t_0 := -\frac{c}{b}\\
t_1 := \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2}\\
t_2 := \frac{\mathsf{fma}\left(-1, b, \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)}\right)}{a \cdot 2}\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_1 \leq -7.311089820331434 \cdot 10^{-265}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;t_1 \leq 0:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_1 \leq 5.78568889785705 \cdot 10^{+298}:\\
\;\;\;\;t_2\\

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


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.4
Target21.1
Herbie14.6
\[\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 3 regimes
  2. if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 4 (*.f64 a c))))) (*.f64 2 a)) < -inf.0 or -7.31108982033143378e-265 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 4 (*.f64 a c))))) (*.f64 2 a)) < 0.0

    1. Initial program 58.3

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

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

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

    if -inf.0 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 4 (*.f64 a c))))) (*.f64 2 a)) < -7.31108982033143378e-265 or 0.0 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 4 (*.f64 a c))))) (*.f64 2 a)) < 5.78568889785704965e298

    1. Initial program 4.1

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

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

    if 5.78568889785704965e298 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 4 (*.f64 a c))))) (*.f64 2 a))

    1. Initial program 63.4

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(b, -1, \mathsf{hypot}\left(b, \sqrt{\left(a \cdot c\right) \cdot -4}\right)\right) \cdot \frac{0.5}{a}} \]
    4. Applied egg-rr37.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2} \leq -\infty:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2} \leq -7.311089820331434 \cdot 10^{-265}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-1, b, \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)}\right)}{a \cdot 2}\\ \mathbf{elif}\;\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2} \leq 0:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - b}{a \cdot 2} \leq 5.78568889785705 \cdot 10^{+298}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-1, b, \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)}\right)}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(b, -1, \mathsf{hypot}\left(b, \sqrt{a} \cdot \sqrt{c \cdot -4}\right)\right) \cdot \frac{0.5}{a}\\ \end{array} \]

Reproduce

herbie shell --seed 2022139 
(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)))