Average Error: 34.0 → 6.7
Time: 9.0s
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 := \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}\\ \mathbf{if}\;b \leq -3.4155395926676587 \cdot 10^{+106}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq -1.1866595270317575 \cdot 10^{-257}:\\ \;\;\;\;\frac{\left(t_0 - b\right) \cdot 0.5}{a}\\ \mathbf{elif}\;b \leq 1.2519627697101639 \cdot 10^{+117}:\\ \;\;\;\;\frac{c \cdot -2}{b + t_0}\\ \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
 (let* ((t_0 (sqrt (fma a (* c -4.0) (* b b)))))
   (if (<= b -3.4155395926676587e+106)
     (- (/ c b) (/ b a))
     (if (<= b -1.1866595270317575e-257)
       (/ (* (- t_0 b) 0.5) a)
       (if (<= b 1.2519627697101639e+117)
         (/ (* c -2.0) (+ b t_0))
         (- (/ 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 t_0 = sqrt(fma(a, (c * -4.0), (b * b)));
	double tmp;
	if (b <= -3.4155395926676587e+106) {
		tmp = (c / b) - (b / a);
	} else if (b <= -1.1866595270317575e-257) {
		tmp = ((t_0 - b) * 0.5) / a;
	} else if (b <= 1.2519627697101639e+117) {
		tmp = (c * -2.0) / (b + t_0);
	} else {
		tmp = -(c / b);
	}
	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 = sqrt(fma(a, Float64(c * -4.0), Float64(b * b)))
	tmp = 0.0
	if (b <= -3.4155395926676587e+106)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	elseif (b <= -1.1866595270317575e-257)
		tmp = Float64(Float64(Float64(t_0 - b) * 0.5) / a);
	elseif (b <= 1.2519627697101639e+117)
		tmp = Float64(Float64(c * -2.0) / Float64(b + t_0));
	else
		tmp = Float64(-Float64(c / b));
	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[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[b, -3.4155395926676587e+106], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, -1.1866595270317575e-257], N[(N[(N[(t$95$0 - b), $MachinePrecision] * 0.5), $MachinePrecision] / a), $MachinePrecision], If[LessEqual[b, 1.2519627697101639e+117], N[(N[(c * -2.0), $MachinePrecision] / N[(b + t$95$0), $MachinePrecision]), $MachinePrecision], (-N[(c / b), $MachinePrecision])]]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
t_0 := \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}\\
\mathbf{if}\;b \leq -3.4155395926676587 \cdot 10^{+106}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \leq -1.1866595270317575 \cdot 10^{-257}:\\
\;\;\;\;\frac{\left(t_0 - b\right) \cdot 0.5}{a}\\

\mathbf{elif}\;b \leq 1.2519627697101639 \cdot 10^{+117}:\\
\;\;\;\;\frac{c \cdot -2}{b + t_0}\\

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


\end{array}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original34.0
Target20.8
Herbie6.7
\[\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 < -3.41553959266765869e106

    1. Initial program 48.5

      \[\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 3.3

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

    if -3.41553959266765869e106 < b < -1.18665952703175748e-257

    1. Initial program 8.6

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

      \[\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 associate-*r/_binary648.6

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

    if -1.18665952703175748e-257 < b < 1.25196276971016391e117

    1. Initial program 32.4

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

      \[\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--_binary6432.5

      \[\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/_binary6432.5

      \[\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. Simplified15.3

      \[\leadsto \frac{\color{blue}{\left(a \cdot \left(c \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 a around 0 9.3

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

    if 1.25196276971016391e117 < b

    1. Initial program 60.5

      \[\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 2.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -3.4155395926676587 \cdot 10^{+106}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq -1.1866595270317575 \cdot 10^{-257}:\\ \;\;\;\;\frac{\left(\sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)} - b\right) \cdot 0.5}{a}\\ \mathbf{elif}\;b \leq 1.2519627697101639 \cdot 10^{+117}:\\ \;\;\;\;\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 2022129 
(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)))