
(FPCore (a b c) :precision binary64 (let* ((t_0 (sqrt (- (* b b) (* (* 4.0 a) c))))) (if (>= b 0.0) (/ (- (- b) t_0) (* 2.0 a)) (/ (* 2.0 c) (+ (- b) t_0)))))
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) / (2.0 * a);
} else {
tmp = (2.0 * c) / (-b + t_0);
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((b * b) - ((4.0d0 * a) * c)))
if (b >= 0.0d0) then
tmp = (-b - t_0) / (2.0d0 * a)
else
tmp = (2.0d0 * c) / (-b + t_0)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt(((b * b) - ((4.0 * a) * c)));
double tmp;
if (b >= 0.0) {
tmp = (-b - t_0) / (2.0 * a);
} else {
tmp = (2.0 * c) / (-b + t_0);
}
return tmp;
}
def code(a, b, c): t_0 = math.sqrt(((b * b) - ((4.0 * a) * c))) tmp = 0 if b >= 0.0: tmp = (-b - t_0) / (2.0 * a) else: tmp = (2.0 * c) / (-b + t_0) return tmp
function code(a, b, c) t_0 = sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c))) tmp = 0.0 if (b >= 0.0) tmp = Float64(Float64(Float64(-b) - t_0) / Float64(2.0 * a)); else tmp = Float64(Float64(2.0 * c) / Float64(Float64(-b) + t_0)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = sqrt(((b * b) - ((4.0 * a) * c))); tmp = 0.0; if (b >= 0.0) tmp = (-b - t_0) / (2.0 * a); else tmp = (2.0 * c) / (-b + t_0); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[GreaterEqual[b, 0.0], N[(N[((-b) - t$95$0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * c), $MachinePrecision] / N[((-b) + t$95$0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\\
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\frac{\left(-b\right) - t\_0}{2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{2 \cdot c}{\left(-b\right) + t\_0}\\
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (let* ((t_0 (sqrt (- (* b b) (* (* 4.0 a) c))))) (if (>= b 0.0) (/ (- (- b) t_0) (* 2.0 a)) (/ (* 2.0 c) (+ (- b) t_0)))))
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) / (2.0 * a);
} else {
tmp = (2.0 * c) / (-b + t_0);
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((b * b) - ((4.0d0 * a) * c)))
if (b >= 0.0d0) then
tmp = (-b - t_0) / (2.0d0 * a)
else
tmp = (2.0d0 * c) / (-b + t_0)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt(((b * b) - ((4.0 * a) * c)));
double tmp;
if (b >= 0.0) {
tmp = (-b - t_0) / (2.0 * a);
} else {
tmp = (2.0 * c) / (-b + t_0);
}
return tmp;
}
def code(a, b, c): t_0 = math.sqrt(((b * b) - ((4.0 * a) * c))) tmp = 0 if b >= 0.0: tmp = (-b - t_0) / (2.0 * a) else: tmp = (2.0 * c) / (-b + t_0) return tmp
function code(a, b, c) t_0 = sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c))) tmp = 0.0 if (b >= 0.0) tmp = Float64(Float64(Float64(-b) - t_0) / Float64(2.0 * a)); else tmp = Float64(Float64(2.0 * c) / Float64(Float64(-b) + t_0)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = sqrt(((b * b) - ((4.0 * a) * c))); tmp = 0.0; if (b >= 0.0) tmp = (-b - t_0) / (2.0 * a); else tmp = (2.0 * c) / (-b + t_0); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[GreaterEqual[b, 0.0], N[(N[((-b) - t$95$0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * c), $MachinePrecision] / N[((-b) + t$95$0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\\
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\frac{\left(-b\right) - t\_0}{2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{2 \cdot c}{\left(-b\right) + t\_0}\\
\end{array}
\end{array}
(FPCore (a b c)
:precision binary64
(let* ((t_0 (fma a (/ c b) (- b))) (t_1 (sqrt (- (* b b) (* (* 4.0 a) c)))))
(if (<= b -1.25e+142)
(if (>= b 0.0) (/ (- (- b) t_1) (* a 2.0)) (/ (* c 2.0) (* 2.0 t_0)))
(if (<= b 1.95e+108)
(if (>= b 0.0)
(fma (/ b a) -0.5 (/ (sqrt (fma b b (* c (* a -4.0)))) (* a -2.0)))
(/ (* c 2.0) (- t_1 b)))
(if (>= b 0.0) (/ t_0 a) (/ (* c 2.0) (- (- b) b)))))))
double code(double a, double b, double c) {
double t_0 = fma(a, (c / b), -b);
double t_1 = sqrt(((b * b) - ((4.0 * a) * c)));
double tmp_1;
if (b <= -1.25e+142) {
double tmp_2;
if (b >= 0.0) {
tmp_2 = (-b - t_1) / (a * 2.0);
} else {
tmp_2 = (c * 2.0) / (2.0 * t_0);
}
tmp_1 = tmp_2;
} else if (b <= 1.95e+108) {
double tmp_3;
if (b >= 0.0) {
tmp_3 = fma((b / a), -0.5, (sqrt(fma(b, b, (c * (a * -4.0)))) / (a * -2.0)));
} else {
tmp_3 = (c * 2.0) / (t_1 - b);
}
tmp_1 = tmp_3;
} else if (b >= 0.0) {
tmp_1 = t_0 / a;
} else {
tmp_1 = (c * 2.0) / (-b - b);
}
return tmp_1;
}
function code(a, b, c) t_0 = fma(a, Float64(c / b), Float64(-b)) t_1 = sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c))) tmp_1 = 0.0 if (b <= -1.25e+142) tmp_2 = 0.0 if (b >= 0.0) tmp_2 = Float64(Float64(Float64(-b) - t_1) / Float64(a * 2.0)); else tmp_2 = Float64(Float64(c * 2.0) / Float64(2.0 * t_0)); end tmp_1 = tmp_2; elseif (b <= 1.95e+108) tmp_3 = 0.0 if (b >= 0.0) tmp_3 = fma(Float64(b / a), -0.5, Float64(sqrt(fma(b, b, Float64(c * Float64(a * -4.0)))) / Float64(a * -2.0))); else tmp_3 = Float64(Float64(c * 2.0) / Float64(t_1 - b)); end tmp_1 = tmp_3; elseif (b >= 0.0) tmp_1 = Float64(t_0 / a); else tmp_1 = Float64(Float64(c * 2.0) / Float64(Float64(-b) - b)); end return tmp_1 end
code[a_, b_, c_] := Block[{t$95$0 = N[(a * N[(c / b), $MachinePrecision] + (-b)), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[b, -1.25e+142], If[GreaterEqual[b, 0.0], N[(N[((-b) - t$95$1), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(c * 2.0), $MachinePrecision] / N[(2.0 * t$95$0), $MachinePrecision]), $MachinePrecision]], If[LessEqual[b, 1.95e+108], If[GreaterEqual[b, 0.0], N[(N[(b / a), $MachinePrecision] * -0.5 + N[(N[Sqrt[N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(a * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(c * 2.0), $MachinePrecision] / N[(t$95$1 - b), $MachinePrecision]), $MachinePrecision]], If[GreaterEqual[b, 0.0], N[(t$95$0 / a), $MachinePrecision], N[(N[(c * 2.0), $MachinePrecision] / N[((-b) - b), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(a, \frac{c}{b}, -b\right)\\
t_1 := \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\\
\mathbf{if}\;b \leq -1.25 \cdot 10^{+142}:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\frac{\left(-b\right) - t\_1}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{2 \cdot t\_0}\\
\end{array}\\
\mathbf{elif}\;b \leq 1.95 \cdot 10^{+108}:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;\mathsf{fma}\left(\frac{b}{a}, -0.5, \frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}{a \cdot -2}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{t\_1 - b}\\
\end{array}\\
\mathbf{elif}\;b \geq 0:\\
\;\;\;\;\frac{t\_0}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{\left(-b\right) - b}\\
\end{array}
\end{array}
if b < -1.25e142Initial program 35.2%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6495.0
Applied rewrites95.0%
Taylor expanded in c around 0
Applied rewrites95.0%
if -1.25e142 < b < 1.94999999999999992e108Initial program 85.3%
Applied rewrites85.3%
if 1.94999999999999992e108 < b Initial program 56.0%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f6456.0
Applied rewrites56.0%
Taylor expanded in b around inf
*-commutativeN/A
lower-*.f6490.8
Applied rewrites90.8%
Taylor expanded in a around 0
+-commutativeN/A
mul-1-negN/A
sub-negN/A
lower-/.f64N/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6491.1
Applied rewrites91.1%
Final simplification88.1%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (sqrt (- (* b b) (* (* 4.0 a) c))))
(t_1 (/ (- (- b) t_0) (* a 2.0)))
(t_2 (fma a (/ c b) (- b))))
(if (<= b -1.25e+142)
(if (>= b 0.0) t_1 (/ (* c 2.0) (* 2.0 t_2)))
(if (<= b 1.95e+108)
(if (>= b 0.0) t_1 (/ (* c 2.0) (- t_0 b)))
(if (>= b 0.0) (/ t_2 a) (/ (* c 2.0) (- (- b) b)))))))
double code(double a, double b, double c) {
double t_0 = sqrt(((b * b) - ((4.0 * a) * c)));
double t_1 = (-b - t_0) / (a * 2.0);
double t_2 = fma(a, (c / b), -b);
double tmp_1;
if (b <= -1.25e+142) {
double tmp_2;
if (b >= 0.0) {
tmp_2 = t_1;
} else {
tmp_2 = (c * 2.0) / (2.0 * t_2);
}
tmp_1 = tmp_2;
} else if (b <= 1.95e+108) {
double tmp_3;
if (b >= 0.0) {
tmp_3 = t_1;
} else {
tmp_3 = (c * 2.0) / (t_0 - b);
}
tmp_1 = tmp_3;
} else if (b >= 0.0) {
tmp_1 = t_2 / a;
} else {
tmp_1 = (c * 2.0) / (-b - b);
}
return tmp_1;
}
function code(a, b, c) t_0 = sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c))) t_1 = Float64(Float64(Float64(-b) - t_0) / Float64(a * 2.0)) t_2 = fma(a, Float64(c / b), Float64(-b)) tmp_1 = 0.0 if (b <= -1.25e+142) tmp_2 = 0.0 if (b >= 0.0) tmp_2 = t_1; else tmp_2 = Float64(Float64(c * 2.0) / Float64(2.0 * t_2)); end tmp_1 = tmp_2; elseif (b <= 1.95e+108) tmp_3 = 0.0 if (b >= 0.0) tmp_3 = t_1; else tmp_3 = Float64(Float64(c * 2.0) / Float64(t_0 - b)); end tmp_1 = tmp_3; elseif (b >= 0.0) tmp_1 = Float64(t_2 / a); else tmp_1 = Float64(Float64(c * 2.0) / Float64(Float64(-b) - b)); end return tmp_1 end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[((-b) - t$95$0), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(a * N[(c / b), $MachinePrecision] + (-b)), $MachinePrecision]}, If[LessEqual[b, -1.25e+142], If[GreaterEqual[b, 0.0], t$95$1, N[(N[(c * 2.0), $MachinePrecision] / N[(2.0 * t$95$2), $MachinePrecision]), $MachinePrecision]], If[LessEqual[b, 1.95e+108], If[GreaterEqual[b, 0.0], t$95$1, N[(N[(c * 2.0), $MachinePrecision] / N[(t$95$0 - b), $MachinePrecision]), $MachinePrecision]], If[GreaterEqual[b, 0.0], N[(t$95$2 / a), $MachinePrecision], N[(N[(c * 2.0), $MachinePrecision] / N[((-b) - b), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\\
t_1 := \frac{\left(-b\right) - t\_0}{a \cdot 2}\\
t_2 := \mathsf{fma}\left(a, \frac{c}{b}, -b\right)\\
\mathbf{if}\;b \leq -1.25 \cdot 10^{+142}:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{2 \cdot t\_2}\\
\end{array}\\
\mathbf{elif}\;b \leq 1.95 \cdot 10^{+108}:\\
\;\;\;\;\begin{array}{l}
\mathbf{if}\;b \geq 0:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{t\_0 - b}\\
\end{array}\\
\mathbf{elif}\;b \geq 0:\\
\;\;\;\;\frac{t\_2}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot 2}{\left(-b\right) - b}\\
\end{array}
\end{array}
if b < -1.25e142Initial program 46.1%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6497.4
Applied rewrites97.4%
Taylor expanded in c around 0
Applied rewrites97.5%
if -1.25e142 < b < 1.94999999999999992e108Initial program 86.9%
if 1.94999999999999992e108 < b Initial program 53.0%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f6453.0
Applied rewrites53.0%
Taylor expanded in b around inf
*-commutativeN/A
lower-*.f6496.4
Applied rewrites96.4%
Taylor expanded in a around 0
+-commutativeN/A
mul-1-negN/A
sub-negN/A
lower-/.f64N/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6496.7
Applied rewrites96.7%
Final simplification90.7%
herbie shell --seed 2024228
(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)))))))