
(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 -5e+151)
(/ (fabs (+ b (fma -2.0 (* a (/ c b)) b))) (* a 2.0))
(if (<= b 2.55e-54)
(/ (- (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+151) {
tmp = fabs((b + fma(-2.0, (a * (c / b)), b))) / (a * 2.0);
} else if (b <= 2.55e-54) {
tmp = (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+151) tmp = Float64(abs(Float64(b + fma(-2.0, Float64(a * Float64(c / b)), b))) / Float64(a * 2.0)); elseif (b <= 2.55e-54) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - b) / Float64(a * 2.0)); else tmp = Float64(c / Float64(-b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -5e+151], N[(N[Abs[N[(b + N[(-2.0 * N[(a * N[(c / b), $MachinePrecision]), $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2.55e-54], 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^{+151}:\\
\;\;\;\;\frac{\left|b + \mathsf{fma}\left(-2, a \cdot \frac{c}{b}, b\right)\right|}{a \cdot 2}\\
\mathbf{elif}\;b \leq 2.55 \cdot 10^{-54}:\\
\;\;\;\;\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 < -5.0000000000000002e151Initial program 43.4%
*-commutative43.4%
Simplified43.4%
Taylor expanded in a around 0 2.1%
add-sqr-sqrt2.1%
sqrt-unprod2.2%
pow22.2%
add-sqr-sqrt26.6%
sqrt-unprod43.5%
sqr-neg43.5%
sqrt-prod0.0%
add-sqr-sqrt43.6%
+-commutative43.6%
fma-define43.6%
associate-/l*43.6%
Applied egg-rr43.6%
unpow243.6%
rem-sqrt-square98.1%
Simplified98.1%
if -5.0000000000000002e151 < b < 2.55000000000000005e-54Initial program 76.2%
if 2.55000000000000005e-54 < b Initial program 18.0%
*-commutative18.0%
Simplified18.0%
Taylor expanded in b around inf 89.0%
associate-*r/89.0%
mul-1-neg89.0%
Simplified89.0%
Final simplification84.7%
(FPCore (a b c)
:precision binary64
(if (<= b -4e+153)
(/ b (- a))
(if (<= b 3.8e-54)
(/ (- (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 <= -4e+153) {
tmp = b / -a;
} else if (b <= 3.8e-54) {
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 <= (-4d+153)) then
tmp = b / -a
else if (b <= 3.8d-54) 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 <= -4e+153) {
tmp = b / -a;
} else if (b <= 3.8e-54) {
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 <= -4e+153: tmp = b / -a elif b <= 3.8e-54: 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 <= -4e+153) tmp = Float64(b / Float64(-a)); elseif (b <= 3.8e-54) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - 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 <= -4e+153) tmp = b / -a; elseif (b <= 3.8e-54) 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, -4e+153], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 3.8e-54], 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 -4 \cdot 10^{+153}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 3.8 \cdot 10^{-54}:\\
\;\;\;\;\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 < -4e153Initial program 43.4%
*-commutative43.4%
Simplified43.4%
Taylor expanded in b around -inf 97.6%
associate-*r/97.6%
mul-1-neg97.6%
Simplified97.6%
if -4e153 < b < 3.8000000000000002e-54Initial program 76.2%
if 3.8000000000000002e-54 < b Initial program 18.0%
*-commutative18.0%
Simplified18.0%
Taylor expanded in b around inf 89.0%
associate-*r/89.0%
mul-1-neg89.0%
Simplified89.0%
Final simplification84.6%
(FPCore (a b c)
:precision binary64
(if (<= b -3.5e+23)
(* b (+ (/ c (pow b 2.0)) (/ -1.0 a)))
(if (<= b 6.6e-55)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.5e+23) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 6.6e-55) {
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 <= (-3.5d+23)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 6.6d-55) 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 <= -3.5e+23) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 6.6e-55) {
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 <= -3.5e+23: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 6.6e-55: 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 <= -3.5e+23) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 6.6e-55) tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -4.0))) - 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 <= -3.5e+23) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 6.6e-55) 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, -3.5e+23], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 6.6e-55], 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 -3.5 \cdot 10^{+23}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 6.6 \cdot 10^{-55}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -3.5000000000000002e23Initial program 67.7%
*-commutative67.7%
Simplified67.7%
Taylor expanded in b around -inf 92.1%
mul-1-neg92.1%
*-commutative92.1%
distribute-rgt-neg-in92.1%
+-commutative92.1%
mul-1-neg92.1%
unsub-neg92.1%
Simplified92.1%
if -3.5000000000000002e23 < b < 6.5999999999999999e-55Initial program 65.5%
*-commutative65.5%
Simplified65.5%
Taylor expanded in b around 0 58.7%
*-commutative58.7%
associate-*r*58.7%
Simplified58.7%
if 6.5999999999999999e-55 < b Initial program 18.0%
*-commutative18.0%
Simplified18.0%
Taylor expanded in b around inf 89.0%
associate-*r/89.0%
mul-1-neg89.0%
Simplified89.0%
Final simplification80.6%
(FPCore (a b c) :precision binary64 (if (<= b 5e-296) (/ b (- a)) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 5e-296) {
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-296) 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-296) {
tmp = b / -a;
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 5e-296: tmp = b / -a else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 5e-296) 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 <= 5e-296) tmp = b / -a; else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 5e-296], N[(b / (-a)), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 5 \cdot 10^{-296}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < 5.0000000000000003e-296Initial program 69.4%
*-commutative69.4%
Simplified69.4%
Taylor expanded in b around -inf 67.7%
associate-*r/67.7%
mul-1-neg67.7%
Simplified67.7%
if 5.0000000000000003e-296 < b Initial program 28.9%
*-commutative28.9%
Simplified28.9%
Taylor expanded in b around inf 66.6%
associate-*r/66.6%
mul-1-neg66.6%
Simplified66.6%
Final simplification67.2%
(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 50.1%
*-commutative50.1%
Simplified50.1%
Taylor expanded in b around -inf 36.7%
associate-*r/36.7%
mul-1-neg36.7%
Simplified36.7%
Final simplification36.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 / 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 50.1%
*-commutative50.1%
Simplified50.1%
clear-num50.1%
inv-pow50.1%
Applied egg-rr22.5%
unpow-122.5%
Simplified22.5%
Taylor expanded in a around 0 2.4%
Final simplification2.4%
herbie shell --seed 2024081
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))