
(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 -2.9e+102)
(- (/ b a))
(if (<= b 2e-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 <= -2.9e+102) {
tmp = -(b / a);
} else if (b <= 2e-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 <= (-2.9d+102)) then
tmp = -(b / a)
else if (b <= 2d-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 <= -2.9e+102) {
tmp = -(b / a);
} else if (b <= 2e-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 <= -2.9e+102: tmp = -(b / a) elif b <= 2e-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 <= -2.9e+102) tmp = Float64(-Float64(b / a)); elseif (b <= 2e-100) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - 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 <= -2.9e+102) tmp = -(b / a); elseif (b <= 2e-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, -2.9e+102], (-N[(b / a), $MachinePrecision]), If[LessEqual[b, 2e-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 -2.9 \cdot 10^{+102}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{elif}\;b \leq 2 \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 < -2.9000000000000002e102Initial program 44.3%
*-commutative44.3%
Simplified44.3%
Taylor expanded in b around -inf 95.9%
associate-*r/95.9%
mul-1-neg95.9%
Simplified95.9%
if -2.9000000000000002e102 < b < 2e-100Initial program 82.4%
if 2e-100 < b Initial program 18.5%
*-commutative18.5%
Simplified18.5%
Taylor expanded in b around inf 80.6%
associate-*r/80.6%
mul-1-neg80.6%
Simplified80.6%
Final simplification84.1%
(FPCore (a b c) :precision binary64 (if (<= b -7.8e-67) (* b (+ (/ c (pow b 2.0)) (/ -1.0 a))) (if (<= b 2e-100) (* (/ 0.5 a) (+ b (sqrt (* a (* c -4.0))))) (/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -7.8e-67) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2e-100) {
tmp = (0.5 / a) * (b + sqrt((a * (c * -4.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 <= (-7.8d-67)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 2d-100) then
tmp = (0.5d0 / a) * (b + sqrt((a * (c * (-4.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 <= -7.8e-67) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2e-100) {
tmp = (0.5 / a) * (b + Math.sqrt((a * (c * -4.0))));
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -7.8e-67: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 2e-100: tmp = (0.5 / a) * (b + math.sqrt((a * (c * -4.0)))) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -7.8e-67) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 2e-100) tmp = Float64(Float64(0.5 / a) * Float64(b + sqrt(Float64(a * Float64(c * -4.0))))); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -7.8e-67) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 2e-100) tmp = (0.5 / a) * (b + sqrt((a * (c * -4.0)))); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -7.8e-67], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2e-100], N[(N[(0.5 / a), $MachinePrecision] * N[(b + N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -7.8 \cdot 10^{-67}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 2 \cdot 10^{-100}:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \left(c \cdot -4\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -7.7999999999999997e-67Initial program 68.0%
*-commutative68.0%
Simplified68.0%
Taylor expanded in b around -inf 86.6%
mul-1-neg86.6%
*-commutative86.6%
distribute-rgt-neg-in86.6%
+-commutative86.6%
mul-1-neg86.6%
unsub-neg86.6%
Simplified86.6%
if -7.7999999999999997e-67 < b < 2e-100Initial program 74.9%
*-commutative74.9%
Simplified74.9%
Taylor expanded in b around 0 73.3%
*-commutative73.3%
associate-*r*73.4%
Simplified73.4%
clear-num73.3%
associate-/r/73.2%
*-commutative73.2%
associate-/r*73.2%
metadata-eval73.2%
+-commutative73.2%
add-sqr-sqrt42.9%
sqrt-unprod73.0%
sqr-neg73.0%
sqrt-unprod30.2%
add-sqr-sqrt72.8%
Applied egg-rr72.8%
if 2e-100 < b Initial program 18.5%
*-commutative18.5%
Simplified18.5%
Taylor expanded in b around inf 80.6%
associate-*r/80.6%
mul-1-neg80.6%
Simplified80.6%
Final simplification80.5%
(FPCore (a b c) :precision binary64 (if (<= b -8.4e-61) (* b (+ (/ c (pow b 2.0)) (/ -1.0 a))) (if (<= b 2e-100) (/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0)) (/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -8.4e-61) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2e-100) {
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 <= (-8.4d-61)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 2d-100) 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 <= -8.4e-61) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2e-100) {
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 <= -8.4e-61: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 2e-100: 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 <= -8.4e-61) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 2e-100) 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 <= -8.4e-61) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 2e-100) 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, -8.4e-61], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2e-100], 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 -8.4 \cdot 10^{-61}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 2 \cdot 10^{-100}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -8.3999999999999996e-61Initial program 67.2%
*-commutative67.2%
Simplified67.2%
Taylor expanded in b around -inf 87.4%
mul-1-neg87.4%
*-commutative87.4%
distribute-rgt-neg-in87.4%
+-commutative87.4%
mul-1-neg87.4%
unsub-neg87.4%
Simplified87.4%
if -8.3999999999999996e-61 < b < 2e-100Initial program 75.7%
*-commutative75.7%
Simplified75.7%
Taylor expanded in b around 0 72.9%
*-commutative72.9%
associate-*r*73.0%
Simplified73.0%
+-commutative73.0%
unsub-neg73.0%
Applied egg-rr73.0%
if 2e-100 < b Initial program 18.5%
*-commutative18.5%
Simplified18.5%
Taylor expanded in b around inf 80.6%
associate-*r/80.6%
mul-1-neg80.6%
Simplified80.6%
Final simplification80.7%
(FPCore (a b c) :precision binary64 (if (<= b 1.26e+36) (- (/ b a)) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 1.26e+36) {
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.26d+36) 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.26e+36) {
tmp = -(b / a);
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 1.26e+36: tmp = -(b / a) else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 1.26e+36) 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.26e+36) tmp = -(b / a); else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 1.26e+36], (-N[(b / a), $MachinePrecision]), N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.26 \cdot 10^{+36}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 1.25999999999999994e36Initial program 65.0%
*-commutative65.0%
Simplified65.0%
Taylor expanded in b around -inf 42.6%
associate-*r/42.6%
mul-1-neg42.6%
Simplified42.6%
if 1.25999999999999994e36 < b Initial program 11.4%
*-commutative11.4%
Simplified11.4%
Taylor expanded in c around 0 90.1%
associate-*r/90.1%
neg-mul-190.1%
distribute-rgt-neg-in90.1%
Simplified90.1%
Taylor expanded in a around 0 93.1%
frac-2neg93.1%
metadata-eval93.1%
un-div-inv93.3%
add-sqr-sqrt0.0%
sqrt-unprod31.7%
sqr-neg31.7%
sqrt-unprod31.4%
add-sqr-sqrt31.4%
Applied egg-rr31.4%
Final simplification39.5%
(FPCore (a b c) :precision binary64 (if (<= b 7.8e-300) (- (/ b a)) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 7.8e-300) {
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 <= 7.8d-300) 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 <= 7.8e-300) {
tmp = -(b / a);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 7.8e-300: tmp = -(b / a) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 7.8e-300) tmp = Float64(-Float64(b / a)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 7.8e-300) tmp = -(b / a); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 7.8e-300], (-N[(b / a), $MachinePrecision]), N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 7.8 \cdot 10^{-300}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < 7.8000000000000002e-300Initial program 72.4%
*-commutative72.4%
Simplified72.4%
Taylor expanded in b around -inf 61.7%
associate-*r/61.7%
mul-1-neg61.7%
Simplified61.7%
if 7.8000000000000002e-300 < b Initial program 28.8%
*-commutative28.8%
Simplified28.8%
Taylor expanded in b around inf 67.4%
associate-*r/67.4%
mul-1-neg67.4%
Simplified67.4%
Final simplification64.6%
(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.0%
inv-pow50.0%
Applied egg-rr27.8%
unpow-127.8%
Simplified27.8%
Taylor expanded in a around 0 2.7%
Final simplification2.7%
(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.1%
*-commutative50.1%
Simplified50.1%
Taylor expanded in c around 0 32.4%
associate-*r/32.4%
neg-mul-132.4%
distribute-rgt-neg-in32.4%
Simplified32.4%
Taylor expanded in a around 0 35.4%
frac-2neg35.4%
metadata-eval35.4%
un-div-inv35.5%
add-sqr-sqrt1.0%
sqrt-unprod10.0%
sqr-neg10.0%
sqrt-unprod9.1%
add-sqr-sqrt10.8%
Applied egg-rr10.8%
Final simplification10.8%
herbie shell --seed 2024077
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))