
(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 -9e+136)
(/ b (- a))
(if (<= b 6.4e-53)
(/ (- (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 <= -9e+136) {
tmp = b / -a;
} else if (b <= 6.4e-53) {
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 <= (-9d+136)) then
tmp = b / -a
else if (b <= 6.4d-53) 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 <= -9e+136) {
tmp = b / -a;
} else if (b <= 6.4e-53) {
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 <= -9e+136: tmp = b / -a elif b <= 6.4e-53: 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 <= -9e+136) tmp = Float64(b / Float64(-a)); elseif (b <= 6.4e-53) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -9e+136) tmp = b / -a; elseif (b <= 6.4e-53) 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, -9e+136], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 6.4e-53], 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 -9 \cdot 10^{+136}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 6.4 \cdot 10^{-53}:\\
\;\;\;\;\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 < -8.9999999999999999e136Initial program 57.4%
*-commutative57.4%
Simplified57.4%
Taylor expanded in b around -inf 95.9%
mul-1-neg95.9%
distribute-neg-frac295.9%
Simplified95.9%
if -8.9999999999999999e136 < b < 6.4000000000000002e-53Initial program 83.2%
if 6.4000000000000002e-53 < b Initial program 15.2%
*-commutative15.2%
Simplified15.2%
Taylor expanded in b around inf 93.6%
associate-*r/93.6%
mul-1-neg93.6%
Simplified93.6%
Final simplification88.8%
(FPCore (a b c)
:precision binary64
(if (<= b -1e-12)
(* b (+ (/ c (pow b 2.0)) (/ -1.0 a)))
(if (<= b 6.4e-53)
(/ (- b (sqrt (* a (* c -4.0)))) (* a -2.0))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1e-12) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 6.4e-53) {
tmp = (b - sqrt((a * (c * -4.0)))) / (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 <= (-1d-12)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 6.4d-53) then
tmp = (b - sqrt((a * (c * (-4.0d0))))) / (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 <= -1e-12) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 6.4e-53) {
tmp = (b - Math.sqrt((a * (c * -4.0)))) / (a * -2.0);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1e-12: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 6.4e-53: tmp = (b - math.sqrt((a * (c * -4.0)))) / (a * -2.0) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1e-12) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 6.4e-53) tmp = Float64(Float64(b - sqrt(Float64(a * Float64(c * -4.0)))) / Float64(a * -2.0)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1e-12) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 6.4e-53) tmp = (b - sqrt((a * (c * -4.0)))) / (a * -2.0); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1e-12], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 6.4e-53], N[(N[(b - N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * -2.0), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1 \cdot 10^{-12}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 6.4 \cdot 10^{-53}:\\
\;\;\;\;\frac{b - \sqrt{a \cdot \left(c \cdot -4\right)}}{a \cdot -2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -9.9999999999999998e-13Initial program 72.7%
*-commutative72.7%
Simplified72.7%
Taylor expanded in b around -inf 89.7%
mul-1-neg89.7%
distribute-rgt-neg-in89.7%
+-commutative89.7%
mul-1-neg89.7%
unsub-neg89.7%
Simplified89.7%
if -9.9999999999999998e-13 < b < 6.4000000000000002e-53Initial program 79.8%
*-commutative79.8%
Simplified79.8%
add-sqr-sqrt79.3%
pow279.3%
pow1/279.3%
sqrt-pow179.4%
sub-neg79.4%
+-commutative79.4%
distribute-lft-neg-in79.4%
*-commutative79.4%
distribute-rgt-neg-in79.4%
metadata-eval79.4%
associate-*r*79.4%
*-commutative79.4%
fma-undefine79.4%
pow279.4%
metadata-eval79.4%
Applied egg-rr79.4%
Taylor expanded in a around inf 71.0%
*-commutative71.0%
associate-*r*71.0%
Simplified71.0%
frac-2neg71.0%
div-inv71.0%
distribute-neg-in71.0%
add-sqr-sqrt43.2%
sqrt-unprod70.7%
sqr-neg70.7%
sqrt-prod27.7%
add-sqr-sqrt69.2%
sub-neg69.2%
add-sqr-sqrt41.5%
sqrt-unprod69.1%
sqr-neg69.1%
sqrt-prod27.7%
add-sqr-sqrt71.0%
pow-pow71.1%
metadata-eval71.1%
pow1/271.1%
distribute-rgt-neg-in71.1%
metadata-eval71.1%
Applied egg-rr71.1%
associate-*r/71.3%
*-rgt-identity71.3%
Simplified71.3%
if 6.4000000000000002e-53 < b Initial program 15.2%
*-commutative15.2%
Simplified15.2%
Taylor expanded in b around inf 93.6%
associate-*r/93.6%
mul-1-neg93.6%
Simplified93.6%
Final simplification84.4%
(FPCore (a b c)
:precision binary64
(if (<= b -1.8e-12)
(* b (+ (/ c (pow b 2.0)) (/ -1.0 a)))
(if (<= b 5.6e-53)
(* (/ 0.5 a) (- (sqrt (* a (* c -4.0))) b))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.8e-12) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 5.6e-53) {
tmp = (0.5 / a) * (sqrt((a * (c * -4.0))) - b);
} 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.8d-12)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 5.6d-53) then
tmp = (0.5d0 / a) * (sqrt((a * (c * (-4.0d0)))) - b)
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.8e-12) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 5.6e-53) {
tmp = (0.5 / a) * (Math.sqrt((a * (c * -4.0))) - b);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.8e-12: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 5.6e-53: tmp = (0.5 / a) * (math.sqrt((a * (c * -4.0))) - b) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.8e-12) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 5.6e-53) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(Float64(a * Float64(c * -4.0))) - b)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.8e-12) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 5.6e-53) tmp = (0.5 / a) * (sqrt((a * (c * -4.0))) - b); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.8e-12], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 5.6e-53], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.8 \cdot 10^{-12}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 5.6 \cdot 10^{-53}:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{a \cdot \left(c \cdot -4\right)} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.8e-12Initial program 72.7%
*-commutative72.7%
Simplified72.7%
Taylor expanded in b around -inf 89.7%
mul-1-neg89.7%
distribute-rgt-neg-in89.7%
+-commutative89.7%
mul-1-neg89.7%
unsub-neg89.7%
Simplified89.7%
if -1.8e-12 < b < 5.59999999999999971e-53Initial program 79.8%
*-commutative79.8%
Simplified79.8%
Applied egg-rr79.5%
sub-neg79.5%
distribute-rgt-out--79.5%
Simplified79.5%
Taylor expanded in a around inf 71.1%
*-commutative71.0%
associate-*r*71.0%
Simplified71.1%
if 5.59999999999999971e-53 < b Initial program 15.2%
*-commutative15.2%
Simplified15.2%
Taylor expanded in b around inf 93.6%
associate-*r/93.6%
mul-1-neg93.6%
Simplified93.6%
Final simplification84.4%
(FPCore (a b c) :precision binary64 (if (<= b 1.16e-262) (/ b (- a)) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 1.16e-262) {
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.16d-262) 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.16e-262) {
tmp = b / -a;
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 1.16e-262: tmp = b / -a else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 1.16e-262) tmp = Float64(b / Float64(-a)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 1.16e-262) tmp = b / -a; else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 1.16e-262], N[(b / (-a)), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.16 \cdot 10^{-262}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < 1.16000000000000001e-262Initial program 77.8%
*-commutative77.8%
Simplified77.8%
Taylor expanded in b around -inf 61.0%
mul-1-neg61.0%
distribute-neg-frac261.0%
Simplified61.0%
if 1.16000000000000001e-262 < b Initial program 30.8%
*-commutative30.8%
Simplified30.8%
Taylor expanded in b around inf 73.6%
associate-*r/73.6%
mul-1-neg73.6%
Simplified73.6%
Final simplification66.7%
(FPCore (a b c) :precision binary64 (if (<= b 2.95e-12) (/ b (- a)) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 2.95e-12) {
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 <= 2.95d-12) 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 <= 2.95e-12) {
tmp = b / -a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 2.95e-12: tmp = b / -a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 2.95e-12) tmp = Float64(b / Float64(-a)); else tmp = Float64(c / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 2.95e-12) tmp = b / -a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 2.95e-12], N[(b / (-a)), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.95 \cdot 10^{-12}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 2.95e-12Initial program 74.1%
*-commutative74.1%
Simplified74.1%
Taylor expanded in b around -inf 48.0%
mul-1-neg48.0%
distribute-neg-frac248.0%
Simplified48.0%
if 2.95e-12 < b Initial program 15.0%
*-commutative15.0%
Simplified15.0%
Applied egg-rr3.7%
unpow-13.7%
associate-/l*3.7%
Simplified3.7%
Taylor expanded in b around -inf 28.3%
*-commutative28.3%
associate-*r/28.3%
Simplified28.3%
Taylor expanded in a around 0 28.1%
(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 56.4%
*-commutative56.4%
Simplified56.4%
Applied egg-rr36.3%
unpow-136.3%
associate-/l*36.3%
Simplified36.3%
Taylor expanded in b around -inf 10.6%
*-commutative10.6%
associate-*r/10.6%
Simplified10.6%
Taylor expanded in a around 0 10.6%
herbie shell --seed 2024085
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))