
(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 -1.6e+93)
(/ (- b) a)
(if (<= b 4.1e-86)
(/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))
(- (/ (- c) b) (* c (/ a (/ (pow b 3.0) c)))))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.6e+93) {
tmp = -b / a;
} else if (b <= 4.1e-86) {
tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - (c * (a / (pow(b, 3.0) / c)));
}
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.6d+93)) then
tmp = -b / a
else if (b <= 4.1d-86) then
tmp = (sqrt(((b * b) - ((a * 4.0d0) * c))) - b) / (a * 2.0d0)
else
tmp = (-c / b) - (c * (a / ((b ** 3.0d0) / c)))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -1.6e+93) {
tmp = -b / a;
} else if (b <= 4.1e-86) {
tmp = (Math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - (c * (a / (Math.pow(b, 3.0) / c)));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.6e+93: tmp = -b / a elif b <= 4.1e-86: tmp = (math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0) else: tmp = (-c / b) - (c * (a / (math.pow(b, 3.0) / c))) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.6e+93) tmp = Float64(Float64(-b) / a); elseif (b <= 4.1e-86) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(c * Float64(a / Float64((b ^ 3.0) / c)))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.6e+93) tmp = -b / a; elseif (b <= 4.1e-86) tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0); else tmp = (-c / b) - (c * (a / ((b ^ 3.0) / c))); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.6e+93], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 4.1e-86], 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[(N[((-c) / b), $MachinePrecision] - N[(c * N[(a / N[(N[Power[b, 3.0], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.6 \cdot 10^{+93}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 4.1 \cdot 10^{-86}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - c \cdot \frac{a}{\frac{{b}^{3}}{c}}\\
\end{array}
\end{array}
if b < -1.6000000000000001e93Initial program 46.7%
Taylor expanded in b around -inf 93.6%
associate-*r/93.6%
mul-1-neg93.6%
Simplified93.6%
if -1.6000000000000001e93 < b < 4.09999999999999979e-86Initial program 79.7%
if 4.09999999999999979e-86 < b Initial program 15.9%
Taylor expanded in b around inf 71.8%
mul-1-neg71.8%
unsub-neg71.8%
associate-*r/71.8%
neg-mul-171.8%
associate-/l*73.5%
unpow273.5%
associate-/r*87.1%
Simplified87.1%
associate-/r/90.0%
Applied egg-rr90.0%
Final simplification86.3%
(FPCore (a b c)
:precision binary64
(if (<= b -5.5e-143)
(- (/ c b) (/ b a))
(if (<= b 8.4e-106)
(/ (- (sqrt (* c (* a -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5.5e-143) {
tmp = (c / b) - (b / a);
} else if (b <= 8.4e-106) {
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 <= (-5.5d-143)) then
tmp = (c / b) - (b / a)
else if (b <= 8.4d-106) 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 <= -5.5e-143) {
tmp = (c / b) - (b / a);
} else if (b <= 8.4e-106) {
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 <= -5.5e-143: tmp = (c / b) - (b / a) elif b <= 8.4e-106: 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 <= -5.5e-143) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 8.4e-106) 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 <= -5.5e-143) tmp = (c / b) - (b / a); elseif (b <= 8.4e-106) 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, -5.5e-143], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 8.4e-106], 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 -5.5 \cdot 10^{-143}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 8.4 \cdot 10^{-106}:\\
\;\;\;\;\frac{\sqrt{c \cdot \left(a \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -5.50000000000000041e-143Initial program 67.8%
Taylor expanded in b around -inf 85.3%
+-commutative85.3%
mul-1-neg85.3%
unsub-neg85.3%
Simplified85.3%
if -5.50000000000000041e-143 < b < 8.40000000000000013e-106Initial program 70.9%
Taylor expanded in b around 0 70.5%
*-commutative70.5%
*-commutative70.5%
associate-*r*70.5%
Simplified70.5%
if 8.40000000000000013e-106 < b Initial program 18.3%
Taylor expanded in b around inf 87.5%
associate-*r/87.5%
neg-mul-187.5%
Simplified87.5%
Final simplification82.5%
(FPCore (a b c) :precision binary64 (if (<= b -1e-309) (- (/ c b) (/ b a)) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -1e-309) {
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 <= (-1d-309)) 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 <= -1e-309) {
tmp = (c / b) - (b / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1e-309: tmp = (c / b) - (b / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1e-309) 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 <= -1e-309) tmp = (c / b) - (b / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1e-309], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1 \cdot 10^{-309}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.000000000000002e-309Initial program 67.4%
Taylor expanded in b around -inf 73.0%
+-commutative73.0%
mul-1-neg73.0%
unsub-neg73.0%
Simplified73.0%
if -1.000000000000002e-309 < b Initial program 34.7%
Taylor expanded in b around inf 66.4%
associate-*r/66.4%
neg-mul-166.4%
Simplified66.4%
Final simplification69.7%
(FPCore (a b c) :precision binary64 (if (<= b 380000000000.0) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 380000000000.0) {
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 <= 380000000000.0d0) 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 <= 380000000000.0) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 380000000000.0: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 380000000000.0) 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 <= 380000000000.0) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 380000000000.0], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 380000000000:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 3.8e11Initial program 66.8%
Taylor expanded in b around -inf 50.2%
associate-*r/50.2%
mul-1-neg50.2%
Simplified50.2%
if 3.8e11 < b Initial program 8.7%
Taylor expanded in b around -inf 2.0%
+-commutative2.0%
mul-1-neg2.0%
unsub-neg2.0%
Simplified2.0%
Taylor expanded in c around inf 34.9%
Final simplification46.0%
(FPCore (a b c) :precision binary64 (if (<= b 3.3e-308) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.3e-308) {
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.3d-308) 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.3e-308) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.3e-308: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.3e-308) 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 <= 3.3e-308) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.3e-308], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.3 \cdot 10^{-308}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 3.2999999999999998e-308Initial program 67.4%
Taylor expanded in b around -inf 72.1%
associate-*r/72.1%
mul-1-neg72.1%
Simplified72.1%
if 3.2999999999999998e-308 < b Initial program 34.7%
Taylor expanded in b around inf 66.4%
associate-*r/66.4%
neg-mul-166.4%
Simplified66.4%
Final simplification69.3%
(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.9%
Taylor expanded in b around -inf 37.2%
+-commutative37.2%
mul-1-neg37.2%
unsub-neg37.2%
Simplified37.2%
Taylor expanded in c around inf 11.7%
Final simplification11.7%
herbie shell --seed 2023285
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))