
(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 5 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 -7.6e+155)
(/ (- b) a)
(if (<= b 1.12e-64)
(/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -7.6e+155) {
tmp = -b / a;
} else if (b <= 1.12e-64) {
tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} 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 <= (-7.6d+155)) then
tmp = -b / a
else if (b <= 1.12d-64) then
tmp = (sqrt(((b * b) - ((a * 4.0d0) * c))) - b) / (a * 2.0d0)
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -7.6e+155) {
tmp = -b / a;
} else if (b <= 1.12e-64) {
tmp = (Math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -7.6e+155: tmp = -b / a elif b <= 1.12e-64: tmp = (math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -7.6e+155) tmp = Float64(Float64(-b) / a); elseif (b <= 1.12e-64) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -7.6e+155) tmp = -b / a; elseif (b <= 1.12e-64) tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -7.6e+155], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.12e-64], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(a * 4.0), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -7.6 \cdot 10^{+155}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.12 \cdot 10^{-64}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -7.6000000000000001e155Initial program 30.6%
neg-sub030.6%
associate-+l-30.6%
sub0-neg30.6%
neg-mul-130.6%
associate-*l/30.6%
*-commutative30.6%
associate-/r*30.6%
/-rgt-identity30.6%
metadata-eval30.6%
Simplified30.7%
Taylor expanded in b around -inf 98.2%
associate-*r/98.2%
mul-1-neg98.2%
Simplified98.2%
if -7.6000000000000001e155 < b < 1.12e-64Initial program 89.8%
if 1.12e-64 < b Initial program 11.3%
neg-sub011.3%
associate-+l-11.3%
sub0-neg11.3%
neg-mul-111.3%
associate-*l/11.3%
*-commutative11.3%
associate-/r*11.3%
/-rgt-identity11.3%
metadata-eval11.3%
Simplified11.3%
Taylor expanded in b around inf 93.8%
mul-1-neg93.8%
distribute-neg-frac93.8%
Simplified93.8%
Final simplification92.8%
(FPCore (a b c)
:precision binary64
(if (<= b -8.5e-98)
(/ (- b) a)
(if (<= b 6.6e-65)
(/ (- (sqrt (* c (* a -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -8.5e-98) {
tmp = -b / a;
} else if (b <= 6.6e-65) {
tmp = (sqrt((c * (a * -4.0))) - b) / (a * 2.0);
} 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 <= (-8.5d-98)) then
tmp = -b / a
else if (b <= 6.6d-65) then
tmp = (sqrt((c * (a * (-4.0d0)))) - b) / (a * 2.0d0)
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -8.5e-98) {
tmp = -b / a;
} else if (b <= 6.6e-65) {
tmp = (Math.sqrt((c * (a * -4.0))) - b) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -8.5e-98: tmp = -b / a elif b <= 6.6e-65: tmp = (math.sqrt((c * (a * -4.0))) - b) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -8.5e-98) tmp = Float64(Float64(-b) / a); elseif (b <= 6.6e-65) tmp = Float64(Float64(sqrt(Float64(c * Float64(a * -4.0))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -8.5e-98) tmp = -b / a; elseif (b <= 6.6e-65) tmp = (sqrt((c * (a * -4.0))) - b) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -8.5e-98], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 6.6e-65], N[(N[(N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -8.5 \cdot 10^{-98}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 6.6 \cdot 10^{-65}:\\
\;\;\;\;\frac{\sqrt{c \cdot \left(a \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -8.4999999999999997e-98Initial program 61.8%
neg-sub061.8%
associate-+l-61.8%
sub0-neg61.8%
neg-mul-161.8%
associate-*l/61.6%
*-commutative61.6%
associate-/r*61.6%
/-rgt-identity61.6%
metadata-eval61.6%
Simplified61.7%
Taylor expanded in b around -inf 85.9%
associate-*r/85.9%
mul-1-neg85.9%
Simplified85.9%
if -8.4999999999999997e-98 < b < 6.6000000000000002e-65Initial program 89.1%
Taylor expanded in b around 0 88.2%
*-commutative88.2%
*-commutative88.2%
associate-*r*88.3%
Simplified88.3%
+-commutative88.3%
unsub-neg88.3%
Applied egg-rr88.3%
associate-*r*88.2%
*-commutative88.2%
associate-*l*88.3%
Simplified88.3%
if 6.6000000000000002e-65 < b Initial program 11.3%
neg-sub011.3%
associate-+l-11.3%
sub0-neg11.3%
neg-mul-111.3%
associate-*l/11.3%
*-commutative11.3%
associate-/r*11.3%
/-rgt-identity11.3%
metadata-eval11.3%
Simplified11.3%
Taylor expanded in b around inf 93.8%
mul-1-neg93.8%
distribute-neg-frac93.8%
Simplified93.8%
Final simplification89.3%
(FPCore (a b c) :precision binary64 (if (<= b 3.1e+25) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.1e+25) {
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 <= 3.1d+25) 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 <= 3.1e+25) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.1e+25: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.1e+25) tmp = Float64(Float64(-b) / a); else tmp = Float64(c / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 3.1e+25) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.1e+25], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.1 \cdot 10^{+25}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 3.0999999999999998e25Initial program 67.4%
neg-sub067.4%
associate-+l-67.4%
sub0-neg67.4%
neg-mul-167.4%
associate-*l/67.3%
*-commutative67.3%
associate-/r*67.3%
/-rgt-identity67.3%
metadata-eval67.3%
Simplified67.3%
Taylor expanded in b around -inf 49.3%
associate-*r/49.3%
mul-1-neg49.3%
Simplified49.3%
if 3.0999999999999998e25 < b Initial program 9.1%
Taylor expanded in b around -inf 2.3%
Taylor expanded in c around inf 31.8%
Final simplification44.3%
(FPCore (a b c) :precision binary64 (if (<= b 1.32e-306) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 1.32e-306) {
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 <= 1.32d-306) 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 <= 1.32e-306) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 1.32e-306: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 1.32e-306) 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 <= 1.32e-306) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 1.32e-306], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.32 \cdot 10^{-306}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 1.32e-306Initial program 68.1%
neg-sub068.1%
associate-+l-68.1%
sub0-neg68.1%
neg-mul-168.1%
associate-*l/68.0%
*-commutative68.0%
associate-/r*68.0%
/-rgt-identity68.0%
metadata-eval68.0%
Simplified68.0%
Taylor expanded in b around -inf 67.0%
associate-*r/67.0%
mul-1-neg67.0%
Simplified67.0%
if 1.32e-306 < b Initial program 32.3%
neg-sub032.3%
associate-+l-32.3%
sub0-neg32.3%
neg-mul-132.3%
associate-*l/32.2%
*-commutative32.2%
associate-/r*32.2%
/-rgt-identity32.2%
metadata-eval32.2%
Simplified32.3%
Taylor expanded in b around inf 70.9%
mul-1-neg70.9%
distribute-neg-frac70.9%
Simplified70.9%
Final simplification68.9%
(FPCore (a b c) :precision binary64 (/ c b))
double code(double a, double b, double c) {
return c / b;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = c / b
end function
public static double code(double a, double b, double c) {
return c / b;
}
def code(a, b, c): return c / b
function code(a, b, c) return Float64(c / b) end
function tmp = code(a, b, c) tmp = c / b; end
code[a_, b_, c_] := N[(c / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{b}
\end{array}
Initial program 50.8%
Taylor expanded in b around -inf 34.0%
Taylor expanded in c around inf 11.3%
Final simplification11.3%
herbie shell --seed 2023216
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))