
(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 7 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 -1.35e+112)
(/ (- b) a)
(if (<= b 3.7e-100)
(/ (- (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 <= -1.35e+112) {
tmp = -b / a;
} else if (b <= 3.7e-100) {
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 <= (-1.35d+112)) then
tmp = -b / a
else if (b <= 3.7d-100) 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 <= -1.35e+112) {
tmp = -b / a;
} else if (b <= 3.7e-100) {
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 <= -1.35e+112: tmp = -b / a elif b <= 3.7e-100: 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 <= -1.35e+112) tmp = Float64(Float64(-b) / a); elseif (b <= 3.7e-100) 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 <= -1.35e+112) tmp = -b / a; elseif (b <= 3.7e-100) 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, -1.35e+112], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 3.7e-100], 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 -1.35 \cdot 10^{+112}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 3.7 \cdot 10^{-100}:\\
\;\;\;\;\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 < -1.3500000000000001e112Initial program 62.7%
neg-sub062.7%
associate-+l-62.7%
sub0-neg62.7%
neg-mul-162.7%
associate-*l/62.7%
*-commutative62.7%
associate-/r*62.7%
/-rgt-identity62.7%
metadata-eval62.7%
Simplified62.7%
Taylor expanded in b around -inf 98.4%
associate-*r/98.4%
mul-1-neg98.4%
Simplified98.4%
if -1.3500000000000001e112 < b < 3.70000000000000018e-100Initial program 81.6%
if 3.70000000000000018e-100 < b Initial program 9.5%
neg-sub09.5%
associate-+l-9.5%
sub0-neg9.5%
neg-mul-19.5%
associate-*l/9.5%
*-commutative9.5%
associate-/r*9.5%
/-rgt-identity9.5%
metadata-eval9.5%
Simplified9.6%
Taylor expanded in b around inf 87.9%
mul-1-neg87.9%
distribute-neg-frac87.9%
Simplified87.9%
Final simplification87.7%
(FPCore (a b c)
:precision binary64
(if (<= b -64000.0)
(- (/ c b) (/ b a))
(if (<= b 1.2e-99)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -64000.0) {
tmp = (c / b) - (b / a);
} else if (b <= 1.2e-99) {
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 <= (-64000.0d0)) then
tmp = (c / b) - (b / a)
else if (b <= 1.2d-99) 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 <= -64000.0) {
tmp = (c / b) - (b / a);
} else if (b <= 1.2e-99) {
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 <= -64000.0: tmp = (c / b) - (b / a) elif b <= 1.2e-99: 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 <= -64000.0) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 1.2e-99) 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 <= -64000.0) tmp = (c / b) - (b / a); elseif (b <= 1.2e-99) 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, -64000.0], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.2e-99], 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 -64000:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 1.2 \cdot 10^{-99}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -64000Initial program 71.0%
neg-sub071.0%
associate-+l-71.0%
sub0-neg71.0%
neg-mul-171.0%
associate-*l/71.0%
*-commutative71.0%
associate-/r*71.0%
/-rgt-identity71.0%
metadata-eval71.0%
Simplified71.0%
Taylor expanded in b around -inf 93.2%
mul-1-neg93.2%
unsub-neg93.2%
Simplified93.2%
if -64000 < b < 1.2e-99Initial program 78.3%
Taylor expanded in b around 0 67.8%
*-commutative67.8%
*-commutative67.8%
associate-*r*67.8%
Simplified67.8%
if 1.2e-99 < b Initial program 9.5%
neg-sub09.5%
associate-+l-9.5%
sub0-neg9.5%
neg-mul-19.5%
associate-*l/9.5%
*-commutative9.5%
associate-/r*9.5%
/-rgt-identity9.5%
metadata-eval9.5%
Simplified9.6%
Taylor expanded in b around inf 87.9%
mul-1-neg87.9%
distribute-neg-frac87.9%
Simplified87.9%
Final simplification82.7%
(FPCore (a b c) :precision binary64 (if (<= b -5e-310) (- (/ c b) (/ b a)) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-310) {
tmp = (c / b) - (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 <= (-5d-310)) then
tmp = (c / b) - (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 <= -5e-310) {
tmp = (c / b) - (b / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-310: tmp = (c / b) - (b / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-310) tmp = Float64(Float64(c / b) - 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 <= -5e-310) tmp = (c / b) - (b / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -4.999999999999985e-310Initial program 77.4%
neg-sub077.4%
associate-+l-77.4%
sub0-neg77.4%
neg-mul-177.4%
associate-*l/77.3%
*-commutative77.3%
associate-/r*77.3%
/-rgt-identity77.3%
metadata-eval77.3%
Simplified77.3%
Taylor expanded in b around -inf 64.8%
mul-1-neg64.8%
unsub-neg64.8%
Simplified64.8%
if -4.999999999999985e-310 < b Initial program 22.4%
neg-sub022.4%
associate-+l-22.4%
sub0-neg22.4%
neg-mul-122.4%
associate-*l/22.3%
*-commutative22.3%
associate-/r*22.3%
/-rgt-identity22.3%
metadata-eval22.3%
Simplified22.4%
Taylor expanded in b around inf 71.1%
mul-1-neg71.1%
distribute-neg-frac71.1%
Simplified71.1%
Final simplification67.6%
(FPCore (a b c) :precision binary64 (if (<= b 2.95e-16) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 2.95e-16) {
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-16) 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-16) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 2.95e-16: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 2.95e-16) 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 <= 2.95e-16) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 2.95e-16], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.95 \cdot 10^{-16}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 2.95000000000000011e-16Initial program 68.9%
neg-sub068.9%
associate-+l-68.9%
sub0-neg68.9%
neg-mul-168.9%
associate-*l/68.8%
*-commutative68.8%
associate-/r*68.8%
/-rgt-identity68.8%
metadata-eval68.8%
Simplified68.8%
Taylor expanded in b around -inf 49.9%
associate-*r/49.9%
mul-1-neg49.9%
Simplified49.9%
if 2.95000000000000011e-16 < b Initial program 9.5%
neg-sub09.5%
associate-+l-9.5%
sub0-neg9.5%
neg-mul-19.5%
associate-*l/9.5%
*-commutative9.5%
associate-/r*9.5%
/-rgt-identity9.5%
metadata-eval9.5%
Simplified9.6%
Taylor expanded in b around -inf 2.1%
Taylor expanded in c around inf 31.7%
Final simplification45.0%
(FPCore (a b c) :precision binary64 (if (<= b -5e-310) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-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 <= (-5d-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 <= -5e-310) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-310: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-310) 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 <= -5e-310) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -4.999999999999985e-310Initial program 77.4%
neg-sub077.4%
associate-+l-77.4%
sub0-neg77.4%
neg-mul-177.4%
associate-*l/77.3%
*-commutative77.3%
associate-/r*77.3%
/-rgt-identity77.3%
metadata-eval77.3%
Simplified77.3%
Taylor expanded in b around -inf 64.7%
associate-*r/64.7%
mul-1-neg64.7%
Simplified64.7%
if -4.999999999999985e-310 < b Initial program 22.4%
neg-sub022.4%
associate-+l-22.4%
sub0-neg22.4%
neg-mul-122.4%
associate-*l/22.3%
*-commutative22.3%
associate-/r*22.3%
/-rgt-identity22.3%
metadata-eval22.3%
Simplified22.4%
Taylor expanded in b around inf 71.1%
mul-1-neg71.1%
distribute-neg-frac71.1%
Simplified71.1%
Final simplification67.5%
(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 / 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 52.9%
neg-sub052.9%
associate-+l-52.9%
sub0-neg52.9%
neg-mul-152.9%
associate-*l/52.8%
*-commutative52.8%
associate-/r*52.8%
/-rgt-identity52.8%
metadata-eval52.8%
Simplified52.8%
associate-*r/52.9%
clear-num52.8%
Applied egg-rr52.8%
Taylor expanded in a around 0 32.8%
mul-1-neg32.8%
unsub-neg32.8%
Simplified32.8%
Taylor expanded in a around inf 2.4%
Final simplification2.4%
(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 52.9%
neg-sub052.9%
associate-+l-52.9%
sub0-neg52.9%
neg-mul-152.9%
associate-*l/52.8%
*-commutative52.8%
associate-/r*52.8%
/-rgt-identity52.8%
metadata-eval52.8%
Simplified52.8%
Taylor expanded in b around -inf 36.0%
Taylor expanded in c around inf 11.0%
Final simplification11.0%
herbie shell --seed 2023178
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))