
(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 -2e+155)
(/ (- b) a)
(if (<= b 6e-105)
(/ (- (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 <= -2e+155) {
tmp = -b / a;
} else if (b <= 6e-105) {
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 <= (-2d+155)) then
tmp = -b / a
else if (b <= 6d-105) 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 <= -2e+155) {
tmp = -b / a;
} else if (b <= 6e-105) {
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 <= -2e+155: tmp = -b / a elif b <= 6e-105: 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 <= -2e+155) tmp = Float64(Float64(-b) / a); elseif (b <= 6e-105) 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 <= -2e+155) tmp = -b / a; elseif (b <= 6e-105) 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, -2e+155], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 6e-105], 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 \cdot 10^{+155}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 6 \cdot 10^{-105}:\\
\;\;\;\;\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.00000000000000001e155Initial program 44.0%
*-commutative44.0%
Simplified44.0%
Taylor expanded in b around -inf 100.0%
associate-*r/100.0%
mul-1-neg100.0%
Simplified100.0%
if -2.00000000000000001e155 < b < 6.0000000000000002e-105Initial program 82.1%
if 6.0000000000000002e-105 < b Initial program 16.3%
*-commutative16.3%
Simplified16.3%
Taylor expanded in b around inf 89.3%
mul-1-neg89.3%
distribute-neg-frac89.3%
Simplified89.3%
Final simplification87.4%
(FPCore (a b c)
:precision binary64
(if (<= b -3.6e-75)
(- (/ c b) (/ b a))
(if (<= b 5.5e-105)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.6e-75) {
tmp = (c / b) - (b / a);
} else if (b <= 5.5e-105) {
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.6d-75)) then
tmp = (c / b) - (b / a)
else if (b <= 5.5d-105) 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.6e-75) {
tmp = (c / b) - (b / a);
} else if (b <= 5.5e-105) {
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.6e-75: tmp = (c / b) - (b / a) elif b <= 5.5e-105: 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.6e-75) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 5.5e-105) 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 <= -3.6e-75) tmp = (c / b) - (b / a); elseif (b <= 5.5e-105) 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.6e-75], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 5.5e-105], 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.6 \cdot 10^{-75}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 5.5 \cdot 10^{-105}:\\
\;\;\;\;\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.6e-75Initial program 72.9%
*-commutative72.9%
Simplified72.9%
Taylor expanded in b around -inf 81.9%
+-commutative81.9%
mul-1-neg81.9%
unsub-neg81.9%
Simplified81.9%
if -3.6e-75 < b < 5.50000000000000029e-105Initial program 73.8%
*-commutative73.8%
Simplified73.8%
prod-diff73.5%
*-commutative73.5%
fma-def73.5%
associate-+l+73.5%
pow273.5%
distribute-lft-neg-in73.5%
*-commutative73.5%
distribute-rgt-neg-in73.5%
metadata-eval73.5%
associate-*r*73.5%
*-commutative73.5%
*-commutative73.5%
fma-udef73.5%
Applied egg-rr73.5%
fma-def73.5%
fma-def73.5%
associate-*l*73.5%
Simplified73.5%
Taylor expanded in b around 0 70.8%
mul-1-neg70.8%
unsub-neg70.8%
distribute-rgt-out71.1%
metadata-eval71.1%
associate-*r*71.1%
*-commutative71.1%
Simplified71.1%
if 5.50000000000000029e-105 < b Initial program 16.3%
*-commutative16.3%
Simplified16.3%
Taylor expanded in b around inf 89.3%
mul-1-neg89.3%
distribute-neg-frac89.3%
Simplified89.3%
Final simplification82.3%
(FPCore (a b c) :precision binary64 (if (<= b -5.5e-74) (- (/ c b) (/ b a)) (if (<= b 2.15e-84) (* 0.5 (/ (sqrt (* a (* c -4.0))) a)) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5.5e-74) {
tmp = (c / b) - (b / a);
} else if (b <= 2.15e-84) {
tmp = 0.5 * (sqrt((a * (c * -4.0))) / 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 <= (-5.5d-74)) then
tmp = (c / b) - (b / a)
else if (b <= 2.15d-84) then
tmp = 0.5d0 * (sqrt((a * (c * (-4.0d0)))) / 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 <= -5.5e-74) {
tmp = (c / b) - (b / a);
} else if (b <= 2.15e-84) {
tmp = 0.5 * (Math.sqrt((a * (c * -4.0))) / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5.5e-74: tmp = (c / b) - (b / a) elif b <= 2.15e-84: tmp = 0.5 * (math.sqrt((a * (c * -4.0))) / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5.5e-74) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 2.15e-84) tmp = Float64(0.5 * Float64(sqrt(Float64(a * Float64(c * -4.0))) / a)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5.5e-74) tmp = (c / b) - (b / a); elseif (b <= 2.15e-84) tmp = 0.5 * (sqrt((a * (c * -4.0))) / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5.5e-74], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2.15e-84], N[(0.5 * N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5.5 \cdot 10^{-74}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 2.15 \cdot 10^{-84}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{a \cdot \left(c \cdot -4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -5.5000000000000001e-74Initial program 72.9%
*-commutative72.9%
Simplified72.9%
Taylor expanded in b around -inf 81.9%
+-commutative81.9%
mul-1-neg81.9%
unsub-neg81.9%
Simplified81.9%
if -5.5000000000000001e-74 < b < 2.1500000000000002e-84Initial program 72.4%
*-commutative72.4%
Simplified72.4%
prod-diff72.2%
*-commutative72.2%
fma-def72.2%
associate-+l+72.2%
pow272.2%
distribute-lft-neg-in72.2%
*-commutative72.2%
distribute-rgt-neg-in72.2%
metadata-eval72.2%
associate-*r*72.2%
*-commutative72.2%
*-commutative72.2%
fma-udef72.2%
Applied egg-rr72.2%
fma-def72.2%
fma-def72.2%
associate-*l*72.2%
Simplified72.2%
Taylor expanded in b around 0 68.7%
associate-*l/68.7%
*-lft-identity68.7%
distribute-rgt-out69.0%
metadata-eval69.0%
associate-*r*69.0%
*-commutative69.0%
Simplified69.0%
if 2.1500000000000002e-84 < b Initial program 14.8%
*-commutative14.8%
Simplified14.8%
Taylor expanded in b around inf 90.8%
mul-1-neg90.8%
distribute-neg-frac90.8%
Simplified90.8%
Final simplification82.1%
(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 65.9%
+-commutative65.9%
mul-1-neg65.9%
unsub-neg65.9%
Simplified65.9%
if -4.999999999999985e-310 < b Initial program 28.1%
*-commutative28.1%
Simplified28.1%
Taylor expanded in b around inf 72.1%
mul-1-neg72.1%
distribute-neg-frac72.1%
Simplified72.1%
Final simplification69.0%
(FPCore (a b c) :precision binary64 (if (<= b 3.1e-283) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.1e-283) {
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.1d-283) 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.1e-283) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.1e-283: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.1e-283) 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.1e-283) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.1e-283], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.1 \cdot 10^{-283}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 3.10000000000000004e-283Initial program 73.4%
*-commutative73.4%
Simplified73.4%
Taylor expanded in b around -inf 63.8%
associate-*r/63.8%
mul-1-neg63.8%
Simplified63.8%
if 3.10000000000000004e-283 < b Initial program 27.4%
*-commutative27.4%
Simplified27.4%
Taylor expanded in b around inf 74.3%
mul-1-neg74.3%
distribute-neg-frac74.3%
Simplified74.3%
Final simplification69.0%
(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(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.8%
*-commutative50.8%
Simplified50.8%
Taylor expanded in b around -inf 33.7%
associate-*r/33.7%
mul-1-neg33.7%
Simplified33.7%
Final simplification33.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.8%
Simplified50.8%
*-un-lft-identity50.8%
*-un-lft-identity50.8%
prod-diff50.8%
*-commutative50.8%
*-un-lft-identity50.8%
fma-def50.8%
*-un-lft-identity50.8%
+-commutative50.8%
add-sqr-sqrt36.5%
sqrt-unprod47.6%
sqr-neg47.6%
sqrt-prod11.3%
add-sqr-sqrt30.9%
pow230.9%
add-sqr-sqrt20.4%
sqrt-unprod30.9%
sqr-neg30.9%
sqrt-prod11.3%
add-sqr-sqrt30.6%
*-commutative30.6%
*-un-lft-identity30.6%
Applied egg-rr30.6%
associate-+l+30.6%
fma-udef30.6%
*-rgt-identity30.6%
Simplified30.6%
Taylor expanded in b around -inf 2.5%
Final simplification2.5%
herbie shell --seed 2023318
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))