
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b + sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b + sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(if (<= b -3.4e+77)
(- (fma (/ (- c) (* b b)) b (/ b a)))
(if (<= b 4700000.0)
(/ (+ (- b) (sqrt (fma (* c -4.0) a (* b b)))) (* 2.0 a))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.4e+77) {
tmp = -fma((-c / (b * b)), b, (b / a));
} else if (b <= 4700000.0) {
tmp = (-b + sqrt(fma((c * -4.0), a, (b * b)))) / (2.0 * a);
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -3.4e+77) tmp = Float64(-fma(Float64(Float64(-c) / Float64(b * b)), b, Float64(b / a))); elseif (b <= 4700000.0) tmp = Float64(Float64(Float64(-b) + sqrt(fma(Float64(c * -4.0), a, Float64(b * b)))) / Float64(2.0 * a)); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -3.4e+77], (-N[(N[((-c) / N[(b * b), $MachinePrecision]), $MachinePrecision] * b + N[(b / a), $MachinePrecision]), $MachinePrecision]), If[LessEqual[b, 4700000.0], N[(N[((-b) + N[Sqrt[N[(N[(c * -4.0), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.4 \cdot 10^{+77}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{-c}{b \cdot b}, b, \frac{b}{a}\right)\\
\mathbf{elif}\;b \leq 4700000:\\
\;\;\;\;\frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(c \cdot -4, a, b \cdot b\right)}}{2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -3.39999999999999997e77Initial program 61.8%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
distribute-rgt-inN/A
associate-*l/N/A
*-lft-identityN/A
lower-fma.f64N/A
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f6494.2
Applied rewrites94.2%
if -3.39999999999999997e77 < b < 4.7e6Initial program 82.9%
lift--.f64N/A
lift-*.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
*-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-eval82.9
Applied rewrites82.9%
if 4.7e6 < b Initial program 10.1%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6487.3
Applied rewrites87.3%
(FPCore (a b c)
:precision binary64
(if (<= b -8e-20)
(- (fma (/ (- c) (* b b)) b (/ b a)))
(if (<= b 9e-13)
(/ (+ (- b) (sqrt (* (* c a) -4.0))) (* 2.0 a))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -8e-20) {
tmp = -fma((-c / (b * b)), b, (b / a));
} else if (b <= 9e-13) {
tmp = (-b + sqrt(((c * a) * -4.0))) / (2.0 * a);
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -8e-20) tmp = Float64(-fma(Float64(Float64(-c) / Float64(b * b)), b, Float64(b / a))); elseif (b <= 9e-13) tmp = Float64(Float64(Float64(-b) + sqrt(Float64(Float64(c * a) * -4.0))) / Float64(2.0 * a)); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -8e-20], (-N[(N[((-c) / N[(b * b), $MachinePrecision]), $MachinePrecision] * b + N[(b / a), $MachinePrecision]), $MachinePrecision]), If[LessEqual[b, 9e-13], N[(N[((-b) + N[Sqrt[N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -8 \cdot 10^{-20}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{-c}{b \cdot b}, b, \frac{b}{a}\right)\\
\mathbf{elif}\;b \leq 9 \cdot 10^{-13}:\\
\;\;\;\;\frac{\left(-b\right) + \sqrt{\left(c \cdot a\right) \cdot -4}}{2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -7.99999999999999956e-20Initial program 70.0%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
distribute-rgt-inN/A
associate-*l/N/A
*-lft-identityN/A
lower-fma.f64N/A
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f6493.0
Applied rewrites93.0%
if -7.99999999999999956e-20 < b < 9e-13Initial program 80.6%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6474.7
Applied rewrites74.7%
if 9e-13 < b Initial program 11.2%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6486.5
Applied rewrites86.5%
(FPCore (a b c) :precision binary64 (if (<= b -4e-310) (- (fma (/ (- c) (* b b)) b (/ b a))) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -4e-310) {
tmp = -fma((-c / (b * b)), b, (b / a));
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -4e-310) tmp = Float64(-fma(Float64(Float64(-c) / Float64(b * b)), b, Float64(b / a))); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -4e-310], (-N[(N[((-c) / N[(b * b), $MachinePrecision]), $MachinePrecision] * b + N[(b / a), $MachinePrecision]), $MachinePrecision]), N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4 \cdot 10^{-310}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{-c}{b \cdot b}, b, \frac{b}{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -3.999999999999988e-310Initial program 74.7%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
distribute-rgt-inN/A
associate-*l/N/A
*-lft-identityN/A
lower-fma.f64N/A
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f6467.8
Applied rewrites67.8%
if -3.999999999999988e-310 < b Initial program 34.1%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6462.7
Applied rewrites62.7%
(FPCore (a b c) :precision binary64 (if (<= b 7e-305) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 7e-305) {
tmp = -b / a;
} else {
tmp = -c / b;
}
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) :: tmp
if (b <= 7d-305) then
tmp = -b / a
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 7e-305) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 7e-305: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 7e-305) tmp = Float64(Float64(-b) / a); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 7e-305) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 7e-305], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 7 \cdot 10^{-305}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 6.9999999999999996e-305Initial program 74.8%
Taylor expanded in b around -inf
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6467.2
Applied rewrites67.2%
if 6.9999999999999996e-305 < b Initial program 33.6%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6463.2
Applied rewrites63.2%
(FPCore (a b c) :precision binary64 (if (<= b -3.3e-302) (/ (- b) a) 0.0))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.3e-302) {
tmp = -b / a;
} else {
tmp = 0.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) :: tmp
if (b <= (-3.3d-302)) then
tmp = -b / a
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -3.3e-302) {
tmp = -b / a;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -3.3e-302: tmp = -b / a else: tmp = 0.0 return tmp
function code(a, b, c) tmp = 0.0 if (b <= -3.3e-302) tmp = Float64(Float64(-b) / a); else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -3.3e-302) tmp = -b / a; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -3.3e-302], N[((-b) / a), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.3 \cdot 10^{-302}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -3.3000000000000002e-302Initial program 75.2%
Taylor expanded in b around -inf
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6468.2
Applied rewrites68.2%
if -3.3000000000000002e-302 < b Initial program 33.9%
lift--.f64N/A
lift-*.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
*-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-eval33.9
Applied rewrites33.9%
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
div-addN/A
lower-+.f64N/A
lower-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
frac-2negN/A
lift-neg.f64N/A
remove-double-negN/A
lower-/.f64N/A
lift-*.f64N/A
distribute-lft-neg-inN/A
metadata-evalN/A
lower-*.f6433.6
Applied rewrites33.6%
Taylor expanded in a around 0
div-addN/A
associate-*r/N/A
associate-*r/N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
+-inverses14.6
Applied rewrites14.6%
(FPCore (a b c) :precision binary64 0.0)
double code(double a, double b, double c) {
return 0.0;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 0.0d0
end function
public static double code(double a, double b, double c) {
return 0.0;
}
def code(a, b, c): return 0.0
function code(a, b, c) return 0.0 end
function tmp = code(a, b, c) tmp = 0.0; end
code[a_, b_, c_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 54.5%
lift--.f64N/A
lift-*.f64N/A
fp-cancel-sub-sign-invN/A
+-commutativeN/A
*-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-eval54.6
Applied rewrites54.6%
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
div-addN/A
lower-+.f64N/A
lower-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
frac-2negN/A
lift-neg.f64N/A
remove-double-negN/A
lower-/.f64N/A
lift-*.f64N/A
distribute-lft-neg-inN/A
metadata-evalN/A
lower-*.f6454.4
Applied rewrites54.4%
Taylor expanded in a around 0
div-addN/A
associate-*r/N/A
associate-*r/N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
+-inverses8.6
Applied rewrites8.6%
herbie shell --seed 2024340
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))