
(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 -7.5e+157)
(/ b (- a))
(if (<= b 1.1e-125)
(/ (- (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 <= -7.5e+157) {
tmp = b / -a;
} else if (b <= 1.1e-125) {
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 <= (-7.5d+157)) then
tmp = b / -a
else if (b <= 1.1d-125) 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 <= -7.5e+157) {
tmp = b / -a;
} else if (b <= 1.1e-125) {
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 <= -7.5e+157: tmp = b / -a elif b <= 1.1e-125: 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 <= -7.5e+157) tmp = Float64(b / Float64(-a)); elseif (b <= 1.1e-125) 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 <= -7.5e+157) tmp = b / -a; elseif (b <= 1.1e-125) 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, -7.5e+157], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 1.1e-125], 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 -7.5 \cdot 10^{+157}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 1.1 \cdot 10^{-125}:\\
\;\;\;\;\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 < -7.5e157Initial program 34.9%
*-commutative34.9%
Simplified34.9%
Taylor expanded in b around -inf 97.9%
associate-*r/97.9%
mul-1-neg97.9%
Simplified97.9%
if -7.5e157 < b < 1.09999999999999997e-125Initial program 80.0%
if 1.09999999999999997e-125 < b Initial program 22.5%
*-commutative22.5%
Simplified22.5%
Taylor expanded in a around 0 87.7%
associate-*r/87.7%
mul-1-neg87.7%
Simplified87.7%
Final simplification86.3%
(FPCore (a b c)
:precision binary64
(if (<= b -1.5e-131)
(/ b (- a))
(if (<= b 1.9e-129)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.5e-131) {
tmp = b / -a;
} else if (b <= 1.9e-129) {
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 <= (-1.5d-131)) then
tmp = b / -a
else if (b <= 1.9d-129) 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 <= -1.5e-131) {
tmp = b / -a;
} else if (b <= 1.9e-129) {
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 <= -1.5e-131: tmp = b / -a elif b <= 1.9e-129: 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 <= -1.5e-131) tmp = Float64(b / Float64(-a)); elseif (b <= 1.9e-129) 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 <= -1.5e-131) tmp = b / -a; elseif (b <= 1.9e-129) 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, -1.5e-131], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 1.9e-129], 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 -1.5 \cdot 10^{-131}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 1.9 \cdot 10^{-129}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.49999999999999998e-131Initial program 66.5%
*-commutative66.5%
Simplified66.5%
Taylor expanded in b around -inf 87.1%
associate-*r/87.1%
mul-1-neg87.1%
Simplified87.1%
if -1.49999999999999998e-131 < b < 1.89999999999999992e-129Initial program 66.7%
*-commutative66.7%
Simplified66.7%
Taylor expanded in a around inf 66.6%
*-commutative66.6%
associate-*r*66.7%
Simplified66.7%
if 1.89999999999999992e-129 < b Initial program 22.5%
*-commutative22.5%
Simplified22.5%
Taylor expanded in a around 0 87.7%
associate-*r/87.7%
mul-1-neg87.7%
Simplified87.7%
Final simplification83.8%
(FPCore (a b c)
:precision binary64
(if (<= b -3.9e-130)
(/ b (- a))
(if (<= b 3.8e-125)
(* (- (sqrt (* a (* c -4.0))) b) (/ 0.5 a))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.9e-130) {
tmp = b / -a;
} else if (b <= 3.8e-125) {
tmp = (sqrt((a * (c * -4.0))) - b) * (0.5 / 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-130)) then
tmp = b / -a
else if (b <= 3.8d-125) then
tmp = (sqrt((a * (c * (-4.0d0)))) - b) * (0.5d0 / 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-130) {
tmp = b / -a;
} else if (b <= 3.8e-125) {
tmp = (Math.sqrt((a * (c * -4.0))) - b) * (0.5 / a);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -3.9e-130: tmp = b / -a elif b <= 3.8e-125: tmp = (math.sqrt((a * (c * -4.0))) - b) * (0.5 / a) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -3.9e-130) tmp = Float64(b / Float64(-a)); elseif (b <= 3.8e-125) tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -4.0))) - b) * Float64(0.5 / a)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -3.9e-130) tmp = b / -a; elseif (b <= 3.8e-125) tmp = (sqrt((a * (c * -4.0))) - b) * (0.5 / a); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -3.9e-130], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 3.8e-125], N[(N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.9 \cdot 10^{-130}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 3.8 \cdot 10^{-125}:\\
\;\;\;\;\left(\sqrt{a \cdot \left(c \cdot -4\right)} - b\right) \cdot \frac{0.5}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -3.9000000000000001e-130Initial program 66.5%
*-commutative66.5%
Simplified66.5%
Taylor expanded in b around -inf 87.1%
associate-*r/87.1%
mul-1-neg87.1%
Simplified87.1%
if -3.9000000000000001e-130 < b < 3.8000000000000001e-125Initial program 66.7%
*-commutative66.7%
Simplified66.7%
div-sub66.7%
sub-neg66.7%
div-inv66.5%
pow266.5%
*-commutative66.5%
associate-/r*66.5%
metadata-eval66.5%
div-inv66.5%
*-commutative66.5%
associate-/r*66.5%
metadata-eval66.5%
Applied egg-rr66.5%
sub-neg66.5%
distribute-rgt-out--66.5%
Simplified66.5%
Taylor expanded in a around inf 66.3%
*-commutative66.3%
associate-*r*66.5%
Simplified66.5%
if 3.8000000000000001e-125 < b Initial program 22.5%
*-commutative22.5%
Simplified22.5%
Taylor expanded in a around 0 87.7%
associate-*r/87.7%
mul-1-neg87.7%
Simplified87.7%
Final simplification83.7%
(FPCore (a b c) :precision binary64 (if (<= b 1.02e-305) (/ b (- a)) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 1.02e-305) {
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.02d-305) 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.02e-305) {
tmp = b / -a;
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 1.02e-305: tmp = b / -a else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 1.02e-305) 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 <= 1.02e-305) tmp = b / -a; else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 1.02e-305], N[(b / (-a)), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.02 \cdot 10^{-305}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < 1.01999999999999994e-305Initial program 67.0%
*-commutative67.0%
Simplified67.0%
Taylor expanded in b around -inf 74.2%
associate-*r/74.2%
mul-1-neg74.2%
Simplified74.2%
if 1.01999999999999994e-305 < b Initial program 30.7%
*-commutative30.7%
Simplified30.7%
Taylor expanded in a around 0 72.1%
associate-*r/72.1%
mul-1-neg72.1%
Simplified72.1%
Final simplification73.1%
(FPCore (a b c) :precision binary64 (if (<= b 15800.0) (/ b (- a)) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 15800.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 <= 15800.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 <= 15800.0) {
tmp = b / -a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 15800.0: tmp = b / -a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 15800.0) tmp = Float64(b / Float64(-a)); else tmp = Float64(c / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 15800.0) tmp = b / -a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 15800.0], N[(b / (-a)), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 15800:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 15800Initial program 62.0%
*-commutative62.0%
Simplified62.0%
Taylor expanded in b around -inf 52.3%
associate-*r/52.3%
mul-1-neg52.3%
Simplified52.3%
if 15800 < b Initial program 16.6%
*-commutative16.6%
Simplified16.6%
Taylor expanded in a around 0 96.1%
associate-*r/96.1%
mul-1-neg96.1%
Simplified96.1%
div-inv95.9%
add-sqr-sqrt43.1%
sqrt-unprod50.0%
sqr-neg50.0%
sqrt-unprod15.4%
add-sqr-sqrt33.9%
Applied egg-rr33.9%
associate-*r/33.9%
*-rgt-identity33.9%
Simplified33.9%
Final simplification46.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 48.0%
*-commutative48.0%
Simplified48.0%
Taylor expanded in a around 0 38.9%
associate-*r/38.9%
mul-1-neg38.9%
Simplified38.9%
div-inv38.8%
add-sqr-sqrt17.8%
sqrt-unprod20.2%
sqr-neg20.2%
sqrt-unprod5.6%
add-sqr-sqrt12.4%
Applied egg-rr12.4%
associate-*r/12.4%
*-rgt-identity12.4%
Simplified12.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 48.0%
*-commutative48.0%
Simplified48.0%
Taylor expanded in b around -inf 37.0%
associate-*r/37.0%
mul-1-neg37.0%
Simplified37.0%
add-sqr-sqrt35.1%
sqrt-unprod26.1%
sqr-neg26.1%
sqrt-prod1.8%
add-sqr-sqrt2.5%
add-cube-cbrt2.5%
pow32.5%
Applied egg-rr2.5%
Taylor expanded in b around 0 2.5%
herbie shell --seed 2024154
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))