
(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 -1e+151)
(/ b (- a))
(if (<= b 1e-37)
(* 0.5 (- (/ (sqrt (+ (* a (* c -4.0)) (* b b))) a) (/ b a)))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1e+151) {
tmp = b / -a;
} else if (b <= 1e-37) {
tmp = 0.5 * ((sqrt(((a * (c * -4.0)) + (b * b))) / a) - (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 <= (-1d+151)) then
tmp = b / -a
else if (b <= 1d-37) then
tmp = 0.5d0 * ((sqrt(((a * (c * (-4.0d0))) + (b * b))) / a) - (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 <= -1e+151) {
tmp = b / -a;
} else if (b <= 1e-37) {
tmp = 0.5 * ((Math.sqrt(((a * (c * -4.0)) + (b * b))) / a) - (b / a));
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1e+151: tmp = b / -a elif b <= 1e-37: tmp = 0.5 * ((math.sqrt(((a * (c * -4.0)) + (b * b))) / a) - (b / a)) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1e+151) tmp = Float64(b / Float64(-a)); elseif (b <= 1e-37) tmp = Float64(0.5 * Float64(Float64(sqrt(Float64(Float64(a * Float64(c * -4.0)) + Float64(b * b))) / a) - 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 <= -1e+151) tmp = b / -a; elseif (b <= 1e-37) tmp = 0.5 * ((sqrt(((a * (c * -4.0)) + (b * b))) / a) - (b / a)); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1e+151], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 1e-37], N[(0.5 * N[(N[(N[Sqrt[N[(N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / a), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1 \cdot 10^{+151}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 10^{-37}:\\
\;\;\;\;0.5 \cdot \left(\frac{\sqrt{a \cdot \left(c \cdot -4\right) + b \cdot b}}{a} - \frac{b}{a}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.00000000000000002e151Initial program 42.5%
*-commutative42.5%
Simplified42.5%
Taylor expanded in b around -inf 100.0%
associate-*r/100.0%
mul-1-neg100.0%
Simplified100.0%
if -1.00000000000000002e151 < b < 1.00000000000000007e-37Initial program 82.4%
*-commutative82.4%
Simplified82.4%
div-sub82.5%
sub-neg82.5%
*-un-lft-identity82.5%
*-commutative82.5%
times-frac82.5%
metadata-eval82.5%
pow282.5%
*-un-lft-identity82.5%
*-commutative82.5%
times-frac82.5%
metadata-eval82.5%
Applied egg-rr82.5%
sub-neg82.5%
distribute-lft-out--82.5%
Simplified82.5%
fma-undefine82.5%
Applied egg-rr82.5%
unpow282.5%
Applied egg-rr82.5%
if 1.00000000000000007e-37 < b Initial program 18.8%
*-commutative18.8%
Simplified18.8%
Taylor expanded in a around 0 90.6%
associate-*r/90.6%
mul-1-neg90.6%
Simplified90.6%
Final simplification87.9%
(FPCore (a b c)
:precision binary64
(if (<= b -5e+155)
(/ b (- a))
(if (<= b 1.45e-38)
(/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e+155) {
tmp = b / -a;
} else if (b <= 1.45e-38) {
tmp = (sqrt(((b * b) - (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 <= (-5d+155)) then
tmp = b / -a
else if (b <= 1.45d-38) then
tmp = (sqrt(((b * b) - (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 <= -5e+155) {
tmp = b / -a;
} else if (b <= 1.45e-38) {
tmp = (Math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e+155: tmp = b / -a elif b <= 1.45e-38: tmp = (math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e+155) tmp = Float64(b / Float64(-a)); elseif (b <= 1.45e-38) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - 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 <= -5e+155) tmp = b / -a; elseif (b <= 1.45e-38) tmp = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e+155], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 1.45e-38], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $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 -5 \cdot 10^{+155}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 1.45 \cdot 10^{-38}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -4.9999999999999999e155Initial program 42.5%
*-commutative42.5%
Simplified42.5%
Taylor expanded in b around -inf 100.0%
associate-*r/100.0%
mul-1-neg100.0%
Simplified100.0%
if -4.9999999999999999e155 < b < 1.44999999999999997e-38Initial program 82.4%
if 1.44999999999999997e-38 < b Initial program 18.8%
*-commutative18.8%
Simplified18.8%
Taylor expanded in a around 0 90.6%
associate-*r/90.6%
mul-1-neg90.6%
Simplified90.6%
Final simplification87.9%
(FPCore (a b c)
:precision binary64
(if (<= b -6.8e-7)
(* b (+ (/ c (pow b 2.0)) (/ -1.0 a)))
(if (<= b 6.8e-40)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -6.8e-7) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 6.8e-40) {
tmp = (sqrt((a * (c * -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 <= (-6.8d-7)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 6.8d-40) then
tmp = (sqrt((a * (c * (-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 <= -6.8e-7) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 6.8e-40) {
tmp = (Math.sqrt((a * (c * -4.0))) - b) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -6.8e-7: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 6.8e-40: tmp = (math.sqrt((a * (c * -4.0))) - b) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -6.8e-7) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 6.8e-40) tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -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 <= -6.8e-7) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 6.8e-40) tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -6.8e-7], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 6.8e-40], N[(N[(N[Sqrt[N[(a * N[(c * -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 -6.8 \cdot 10^{-7}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 6.8 \cdot 10^{-40}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -6.79999999999999948e-7Initial program 69.8%
*-commutative69.8%
Simplified69.8%
Taylor expanded in b around -inf 90.4%
mul-1-neg90.4%
*-commutative90.4%
distribute-rgt-neg-in90.4%
+-commutative90.4%
mul-1-neg90.4%
unsub-neg90.4%
Simplified90.4%
if -6.79999999999999948e-7 < b < 6.79999999999999968e-40Initial program 76.8%
*-commutative76.8%
Simplified76.8%
Taylor expanded in a around inf 68.0%
*-commutative68.0%
associate-*r*68.0%
Simplified68.0%
if 6.79999999999999968e-40 < b Initial program 18.8%
*-commutative18.8%
Simplified18.8%
Taylor expanded in a around 0 90.6%
associate-*r/90.6%
mul-1-neg90.6%
Simplified90.6%
Final simplification83.0%
(FPCore (a b c) :precision binary64 (if (<= b -1e-310) (/ b (- a)) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -1e-310) {
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 <= (-1d-310)) 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 <= -1e-310) {
tmp = b / -a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1e-310: tmp = b / -a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1e-310) tmp = Float64(b / Float64(-a)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1e-310) tmp = b / -a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1e-310], N[(b / (-a)), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1 \cdot 10^{-310}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -9.999999999999969e-311Initial program 74.0%
*-commutative74.0%
Simplified74.0%
Taylor expanded in b around -inf 67.5%
associate-*r/67.5%
mul-1-neg67.5%
Simplified67.5%
if -9.999999999999969e-311 < b Initial program 35.7%
*-commutative35.7%
Simplified35.7%
Taylor expanded in a around 0 67.9%
associate-*r/67.9%
mul-1-neg67.9%
Simplified67.9%
Final simplification67.7%
(FPCore (a b c) :precision binary64 (/ b (- a)))
double code(double a, double b, double c) {
return b / -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 / -a
end function
public static double code(double a, double b, double c) {
return b / -a;
}
def code(a, b, c): return b / -a
function code(a, b, c) return Float64(b / Float64(-a)) end
function tmp = code(a, b, c) tmp = b / -a; end
code[a_, b_, c_] := N[(b / (-a)), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{-a}
\end{array}
Initial program 54.4%
*-commutative54.4%
Simplified54.4%
Taylor expanded in b around -inf 34.4%
associate-*r/34.4%
mul-1-neg34.4%
Simplified34.4%
Final simplification34.4%
herbie shell --seed 2024173
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))