
(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 -4.8e+135)
(/ (- b) a)
(if (<= b 1.05e-152)
(/ (- (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 <= -4.8e+135) {
tmp = -b / a;
} else if (b <= 1.05e-152) {
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 <= (-4.8d+135)) then
tmp = -b / a
else if (b <= 1.05d-152) 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 <= -4.8e+135) {
tmp = -b / a;
} else if (b <= 1.05e-152) {
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 <= -4.8e+135: tmp = -b / a elif b <= 1.05e-152: 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 <= -4.8e+135) tmp = Float64(Float64(-b) / a); elseif (b <= 1.05e-152) 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 <= -4.8e+135) tmp = -b / a; elseif (b <= 1.05e-152) 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, -4.8e+135], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.05e-152], 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 -4.8 \cdot 10^{+135}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.05 \cdot 10^{-152}:\\
\;\;\;\;\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 < -4.79999999999999995e135Initial program 53.0%
*-commutative53.0%
Simplified53.0%
Taylor expanded in b around -inf 98.3%
associate-*r/98.3%
mul-1-neg98.3%
Simplified98.3%
if -4.79999999999999995e135 < b < 1.04999999999999999e-152Initial program 86.7%
if 1.04999999999999999e-152 < b Initial program 22.2%
*-commutative22.2%
Simplified22.2%
Taylor expanded in b around inf 83.5%
mul-1-neg83.5%
distribute-neg-frac83.5%
Simplified83.5%
Final simplification87.8%
(FPCore (a b c)
:precision binary64
(if (<= b -10000.0)
(- (/ c b) (/ b a))
(if (<= b 1.05e-152)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -10000.0) {
tmp = (c / b) - (b / a);
} else if (b <= 1.05e-152) {
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 <= (-10000.0d0)) then
tmp = (c / b) - (b / a)
else if (b <= 1.05d-152) 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 <= -10000.0) {
tmp = (c / b) - (b / a);
} else if (b <= 1.05e-152) {
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 <= -10000.0: tmp = (c / b) - (b / a) elif b <= 1.05e-152: 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 <= -10000.0) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 1.05e-152) 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 <= -10000.0) tmp = (c / b) - (b / a); elseif (b <= 1.05e-152) 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, -10000.0], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.05e-152], 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 -10000:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 1.05 \cdot 10^{-152}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1e4Initial program 68.8%
*-commutative68.8%
Simplified68.8%
Taylor expanded in b around -inf 94.5%
+-commutative94.5%
mul-1-neg94.5%
unsub-neg94.5%
Simplified94.5%
if -1e4 < b < 1.04999999999999999e-152Initial program 81.1%
*-commutative81.1%
Simplified81.1%
Taylor expanded in b around 0 68.8%
*-commutative68.8%
associate-*r*68.8%
Simplified68.8%
if 1.04999999999999999e-152 < b Initial program 22.2%
*-commutative22.2%
Simplified22.2%
Taylor expanded in b around inf 83.5%
mul-1-neg83.5%
distribute-neg-frac83.5%
Simplified83.5%
Final simplification83.3%
(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 74.1%
*-commutative74.1%
Simplified74.1%
Taylor expanded in b around -inf 71.6%
+-commutative71.6%
mul-1-neg71.6%
unsub-neg71.6%
Simplified71.6%
if -4.999999999999985e-310 < b Initial program 30.6%
*-commutative30.6%
Simplified30.6%
Taylor expanded in b around inf 71.8%
mul-1-neg71.8%
distribute-neg-frac71.8%
Simplified71.8%
Final simplification71.7%
(FPCore (a b c) :precision binary64 (if (<= b 1.25e+33) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 1.25e+33) {
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.25d+33) 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.25e+33) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 1.25e+33: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 1.25e+33) 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 <= 1.25e+33) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 1.25e+33], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.25 \cdot 10^{+33}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 1.24999999999999993e33Initial program 68.1%
*-commutative68.1%
Simplified68.1%
Taylor expanded in b around -inf 52.1%
associate-*r/52.1%
mul-1-neg52.1%
Simplified52.1%
if 1.24999999999999993e33 < b Initial program 16.1%
*-commutative16.1%
Simplified16.1%
Taylor expanded in b around inf 75.1%
associate-/l*80.5%
Simplified80.5%
*-un-lft-identity80.5%
*-commutative80.5%
times-frac80.5%
metadata-eval80.5%
div-inv80.6%
clear-num80.8%
Applied egg-rr80.8%
*-lft-identity80.8%
associate-*r/80.8%
neg-mul-180.8%
distribute-rgt-neg-in80.8%
Simplified80.8%
associate-/l*85.6%
div-inv85.5%
div-inv84.1%
add-sqr-sqrt58.9%
sqrt-unprod56.5%
sqr-neg56.5%
sqrt-unprod30.9%
add-sqr-sqrt33.3%
clear-num33.3%
Applied egg-rr33.3%
Taylor expanded in a around 0 33.1%
Final simplification46.4%
(FPCore (a b c) :precision binary64 (if (<= b 3.9e-308) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.9e-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.9d-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.9e-308) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.9e-308: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.9e-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.9e-308) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.9e-308], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.9 \cdot 10^{-308}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 3.8999999999999999e-308Initial program 74.1%
*-commutative74.1%
Simplified74.1%
Taylor expanded in b around -inf 71.1%
associate-*r/71.1%
mul-1-neg71.1%
Simplified71.1%
if 3.8999999999999999e-308 < b Initial program 30.6%
*-commutative30.6%
Simplified30.6%
Taylor expanded in b around inf 71.8%
mul-1-neg71.8%
distribute-neg-frac71.8%
Simplified71.8%
Final simplification71.4%
(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.5%
*-commutative52.5%
Simplified52.5%
Applied egg-rr32.7%
Taylor expanded in c around 0 2.5%
pow-base-12.5%
*-lft-identity2.5%
Simplified2.5%
Final simplification2.5%
(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.5%
*-commutative52.5%
Simplified52.5%
Taylor expanded in b around inf 28.8%
associate-/l*31.6%
Simplified31.6%
*-un-lft-identity31.6%
*-commutative31.6%
times-frac31.6%
metadata-eval31.6%
div-inv31.6%
clear-num31.7%
Applied egg-rr31.7%
*-lft-identity31.7%
associate-*r/31.7%
neg-mul-131.7%
distribute-rgt-neg-in31.7%
Simplified31.7%
associate-/l*34.5%
div-inv34.4%
div-inv34.0%
add-sqr-sqrt23.6%
sqrt-unprod22.9%
sqr-neg22.9%
sqrt-unprod10.5%
add-sqr-sqrt12.6%
clear-num12.6%
Applied egg-rr12.6%
Taylor expanded in a around 0 12.3%
Final simplification12.3%
herbie shell --seed 2024039
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))