
(FPCore (a b_2 c) :precision binary64 (/ (+ (- b_2) (sqrt (- (* b_2 b_2) (* a c)))) a))
double code(double a, double b_2, double c) {
return (-b_2 + sqrt(((b_2 * b_2) - (a * c)))) / a;
}
real(8) function code(a, b_2, c)
real(8), intent (in) :: a
real(8), intent (in) :: b_2
real(8), intent (in) :: c
code = (-b_2 + sqrt(((b_2 * b_2) - (a * c)))) / a
end function
public static double code(double a, double b_2, double c) {
return (-b_2 + Math.sqrt(((b_2 * b_2) - (a * c)))) / a;
}
def code(a, b_2, c): return (-b_2 + math.sqrt(((b_2 * b_2) - (a * c)))) / a
function code(a, b_2, c) return Float64(Float64(Float64(-b_2) + sqrt(Float64(Float64(b_2 * b_2) - Float64(a * c)))) / a) end
function tmp = code(a, b_2, c) tmp = (-b_2 + sqrt(((b_2 * b_2) - (a * c)))) / a; end
code[a_, b$95$2_, c_] := N[(N[((-b$95$2) + N[Sqrt[N[(N[(b$95$2 * b$95$2), $MachinePrecision] - N[(a * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\_2\right) + \sqrt{b\_2 \cdot b\_2 - a \cdot c}}{a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b_2 c) :precision binary64 (/ (+ (- b_2) (sqrt (- (* b_2 b_2) (* a c)))) a))
double code(double a, double b_2, double c) {
return (-b_2 + sqrt(((b_2 * b_2) - (a * c)))) / a;
}
real(8) function code(a, b_2, c)
real(8), intent (in) :: a
real(8), intent (in) :: b_2
real(8), intent (in) :: c
code = (-b_2 + sqrt(((b_2 * b_2) - (a * c)))) / a
end function
public static double code(double a, double b_2, double c) {
return (-b_2 + Math.sqrt(((b_2 * b_2) - (a * c)))) / a;
}
def code(a, b_2, c): return (-b_2 + math.sqrt(((b_2 * b_2) - (a * c)))) / a
function code(a, b_2, c) return Float64(Float64(Float64(-b_2) + sqrt(Float64(Float64(b_2 * b_2) - Float64(a * c)))) / a) end
function tmp = code(a, b_2, c) tmp = (-b_2 + sqrt(((b_2 * b_2) - (a * c)))) / a; end
code[a_, b$95$2_, c_] := N[(N[((-b$95$2) + N[Sqrt[N[(N[(b$95$2 * b$95$2), $MachinePrecision] - N[(a * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\_2\right) + \sqrt{b\_2 \cdot b\_2 - a \cdot c}}{a}
\end{array}
(FPCore (a b_2 c)
:precision binary64
(if (<= b_2 -2.55e+156)
(fma (/ b_2 a) -2.0 (/ (* c 0.5) b_2))
(if (<= b_2 3e-66)
(/ (- (sqrt (fma c (- a) (* b_2 b_2))) b_2) a)
(/ c (* b_2 -2.0)))))
double code(double a, double b_2, double c) {
double tmp;
if (b_2 <= -2.55e+156) {
tmp = fma((b_2 / a), -2.0, ((c * 0.5) / b_2));
} else if (b_2 <= 3e-66) {
tmp = (sqrt(fma(c, -a, (b_2 * b_2))) - b_2) / a;
} else {
tmp = c / (b_2 * -2.0);
}
return tmp;
}
function code(a, b_2, c) tmp = 0.0 if (b_2 <= -2.55e+156) tmp = fma(Float64(b_2 / a), -2.0, Float64(Float64(c * 0.5) / b_2)); elseif (b_2 <= 3e-66) tmp = Float64(Float64(sqrt(fma(c, Float64(-a), Float64(b_2 * b_2))) - b_2) / a); else tmp = Float64(c / Float64(b_2 * -2.0)); end return tmp end
code[a_, b$95$2_, c_] := If[LessEqual[b$95$2, -2.55e+156], N[(N[(b$95$2 / a), $MachinePrecision] * -2.0 + N[(N[(c * 0.5), $MachinePrecision] / b$95$2), $MachinePrecision]), $MachinePrecision], If[LessEqual[b$95$2, 3e-66], N[(N[(N[Sqrt[N[(c * (-a) + N[(b$95$2 * b$95$2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b$95$2), $MachinePrecision] / a), $MachinePrecision], N[(c / N[(b$95$2 * -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b\_2 \leq -2.55 \cdot 10^{+156}:\\
\;\;\;\;\mathsf{fma}\left(\frac{b\_2}{a}, -2, \frac{c \cdot 0.5}{b\_2}\right)\\
\mathbf{elif}\;b\_2 \leq 3 \cdot 10^{-66}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(c, -a, b\_2 \cdot b\_2\right)} - b\_2}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b\_2 \cdot -2}\\
\end{array}
\end{array}
if b_2 < -2.55000000000000007e156Initial program 28.6%
Taylor expanded in b_2 around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-*.f64N/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6496.6
Applied rewrites96.6%
Taylor expanded in a around inf
Applied rewrites97.7%
if -2.55000000000000007e156 < b_2 < 3.0000000000000002e-66Initial program 81.2%
Taylor expanded in b_2 around 0
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f6454.6
Applied rewrites54.6%
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6454.6
Applied rewrites54.6%
Taylor expanded in b_2 around 0
sub-negN/A
mul-1-negN/A
+-commutativeN/A
mul-1-negN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
lower-neg.f64N/A
unpow2N/A
lower-*.f6481.2
Applied rewrites81.2%
if 3.0000000000000002e-66 < b_2 Initial program 18.0%
Taylor expanded in b_2 around inf
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
associate-*r/N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6488.7
Applied rewrites88.7%
Applied rewrites89.0%
(FPCore (a b_2 c) :precision binary64 (if (<= b_2 -3.45e-74) (fma (/ b_2 a) -2.0 (/ (* c 0.5) b_2)) (if (<= b_2 1.5e-66) (/ (- (sqrt (* a (- c))) b_2) a) (/ c (* b_2 -2.0)))))
double code(double a, double b_2, double c) {
double tmp;
if (b_2 <= -3.45e-74) {
tmp = fma((b_2 / a), -2.0, ((c * 0.5) / b_2));
} else if (b_2 <= 1.5e-66) {
tmp = (sqrt((a * -c)) - b_2) / a;
} else {
tmp = c / (b_2 * -2.0);
}
return tmp;
}
function code(a, b_2, c) tmp = 0.0 if (b_2 <= -3.45e-74) tmp = fma(Float64(b_2 / a), -2.0, Float64(Float64(c * 0.5) / b_2)); elseif (b_2 <= 1.5e-66) tmp = Float64(Float64(sqrt(Float64(a * Float64(-c))) - b_2) / a); else tmp = Float64(c / Float64(b_2 * -2.0)); end return tmp end
code[a_, b$95$2_, c_] := If[LessEqual[b$95$2, -3.45e-74], N[(N[(b$95$2 / a), $MachinePrecision] * -2.0 + N[(N[(c * 0.5), $MachinePrecision] / b$95$2), $MachinePrecision]), $MachinePrecision], If[LessEqual[b$95$2, 1.5e-66], N[(N[(N[Sqrt[N[(a * (-c)), $MachinePrecision]], $MachinePrecision] - b$95$2), $MachinePrecision] / a), $MachinePrecision], N[(c / N[(b$95$2 * -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b\_2 \leq -3.45 \cdot 10^{-74}:\\
\;\;\;\;\mathsf{fma}\left(\frac{b\_2}{a}, -2, \frac{c \cdot 0.5}{b\_2}\right)\\
\mathbf{elif}\;b\_2 \leq 1.5 \cdot 10^{-66}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(-c\right)} - b\_2}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b\_2 \cdot -2}\\
\end{array}
\end{array}
if b_2 < -3.4499999999999999e-74Initial program 69.1%
Taylor expanded in b_2 around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-*.f64N/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6486.4
Applied rewrites86.4%
Taylor expanded in a around inf
Applied rewrites86.7%
if -3.4499999999999999e-74 < b_2 < 1.5000000000000001e-66Initial program 74.1%
Taylor expanded in b_2 around 0
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f6468.7
Applied rewrites68.7%
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6468.7
Applied rewrites68.7%
if 1.5000000000000001e-66 < b_2 Initial program 17.5%
Taylor expanded in b_2 around inf
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
associate-*r/N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6486.1
Applied rewrites86.1%
Applied rewrites86.3%
(FPCore (a b_2 c)
:precision binary64
(let* ((t_0 (* (sqrt (fabs a)) (sqrt (fabs c))))
(t_1
(if (== (copysign a c) a)
(* (sqrt (- (fabs b_2) t_0)) (sqrt (+ (fabs b_2) t_0)))
(hypot b_2 t_0))))
(if (< b_2 0.0) (/ (- t_1 b_2) a) (/ (- c) (+ b_2 t_1)))))
double code(double a, double b_2, double c) {
double t_0 = sqrt(fabs(a)) * sqrt(fabs(c));
double tmp;
if (copysign(a, c) == a) {
tmp = sqrt((fabs(b_2) - t_0)) * sqrt((fabs(b_2) + t_0));
} else {
tmp = hypot(b_2, t_0);
}
double t_1 = tmp;
double tmp_1;
if (b_2 < 0.0) {
tmp_1 = (t_1 - b_2) / a;
} else {
tmp_1 = -c / (b_2 + t_1);
}
return tmp_1;
}
public static double code(double a, double b_2, double c) {
double t_0 = Math.sqrt(Math.abs(a)) * Math.sqrt(Math.abs(c));
double tmp;
if (Math.copySign(a, c) == a) {
tmp = Math.sqrt((Math.abs(b_2) - t_0)) * Math.sqrt((Math.abs(b_2) + t_0));
} else {
tmp = Math.hypot(b_2, t_0);
}
double t_1 = tmp;
double tmp_1;
if (b_2 < 0.0) {
tmp_1 = (t_1 - b_2) / a;
} else {
tmp_1 = -c / (b_2 + t_1);
}
return tmp_1;
}
def code(a, b_2, c): t_0 = math.sqrt(math.fabs(a)) * math.sqrt(math.fabs(c)) tmp = 0 if math.copysign(a, c) == a: tmp = math.sqrt((math.fabs(b_2) - t_0)) * math.sqrt((math.fabs(b_2) + t_0)) else: tmp = math.hypot(b_2, t_0) t_1 = tmp tmp_1 = 0 if b_2 < 0.0: tmp_1 = (t_1 - b_2) / a else: tmp_1 = -c / (b_2 + t_1) return tmp_1
function code(a, b_2, c) t_0 = Float64(sqrt(abs(a)) * sqrt(abs(c))) tmp = 0.0 if (copysign(a, c) == a) tmp = Float64(sqrt(Float64(abs(b_2) - t_0)) * sqrt(Float64(abs(b_2) + t_0))); else tmp = hypot(b_2, t_0); end t_1 = tmp tmp_1 = 0.0 if (b_2 < 0.0) tmp_1 = Float64(Float64(t_1 - b_2) / a); else tmp_1 = Float64(Float64(-c) / Float64(b_2 + t_1)); end return tmp_1 end
function tmp_3 = code(a, b_2, c) t_0 = sqrt(abs(a)) * sqrt(abs(c)); tmp = 0.0; if ((sign(c) * abs(a)) == a) tmp = sqrt((abs(b_2) - t_0)) * sqrt((abs(b_2) + t_0)); else tmp = hypot(b_2, t_0); end t_1 = tmp; tmp_2 = 0.0; if (b_2 < 0.0) tmp_2 = (t_1 - b_2) / a; else tmp_2 = -c / (b_2 + t_1); end tmp_3 = tmp_2; end
code[a_, b$95$2_, c_] := Block[{t$95$0 = N[(N[Sqrt[N[Abs[a], $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[Abs[c], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = If[Equal[N[With[{TMP1 = Abs[a], TMP2 = Sign[c]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision], a], N[(N[Sqrt[N[(N[Abs[b$95$2], $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(N[Abs[b$95$2], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[b$95$2 ^ 2 + t$95$0 ^ 2], $MachinePrecision]]}, If[Less[b$95$2, 0.0], N[(N[(t$95$1 - b$95$2), $MachinePrecision] / a), $MachinePrecision], N[((-c) / N[(b$95$2 + t$95$1), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\left|a\right|} \cdot \sqrt{\left|c\right|}\\
t_1 := \begin{array}{l}
\mathbf{if}\;\mathsf{copysign}\left(a, c\right) = a:\\
\;\;\;\;\sqrt{\left|b\_2\right| - t\_0} \cdot \sqrt{\left|b\_2\right| + t\_0}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(b\_2, t\_0\right)\\
\end{array}\\
\mathbf{if}\;b\_2 < 0:\\
\;\;\;\;\frac{t\_1 - b\_2}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b\_2 + t\_1}\\
\end{array}
\end{array}
herbie shell --seed 2024222
(FPCore (a b_2 c)
:name "quad2p (problem 3.2.1, positive)"
:precision binary64
:herbie-expected 10
:alt
(! :herbie-platform default (let ((sqtD (let ((x (* (sqrt (fabs a)) (sqrt (fabs c))))) (if (== (copysign a c) a) (* (sqrt (- (fabs b_2) x)) (sqrt (+ (fabs b_2) x))) (hypot b_2 x))))) (if (< b_2 0) (/ (- sqtD b_2) a) (/ (- c) (+ b_2 sqtD)))))
(/ (+ (- b_2) (sqrt (- (* b_2 b_2) (* a c)))) a))