
(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 -4.4e+55)
(- (/ c b) (/ b a))
(if (<= b 1.15e-40)
(/ (- (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 <= -4.4e+55) {
tmp = (c / b) - (b / a);
} else if (b <= 1.15e-40) {
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 <= (-4.4d+55)) then
tmp = (c / b) - (b / a)
else if (b <= 1.15d-40) 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 <= -4.4e+55) {
tmp = (c / b) - (b / a);
} else if (b <= 1.15e-40) {
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 <= -4.4e+55: tmp = (c / b) - (b / a) elif b <= 1.15e-40: 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 <= -4.4e+55) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 1.15e-40) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 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 <= -4.4e+55) tmp = (c / b) - (b / a); elseif (b <= 1.15e-40) 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, -4.4e+55], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.15e-40], 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.4 \cdot 10^{+55}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 1.15 \cdot 10^{-40}:\\
\;\;\;\;\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 < -4.40000000000000021e55Initial program 63.9%
*-commutative63.9%
Simplified63.9%
Taylor expanded in b around -inf 96.0%
+-commutative96.0%
mul-1-neg96.0%
unsub-neg96.0%
Simplified96.0%
if -4.40000000000000021e55 < b < 1.15e-40Initial program 77.9%
if 1.15e-40 < b Initial program 15.6%
*-commutative15.6%
Simplified15.6%
Taylor expanded in b around inf 90.0%
mul-1-neg90.0%
distribute-neg-frac90.0%
Simplified90.0%
Final simplification86.7%
(FPCore (a b c)
:precision binary64
(if (<= b -1.15e-68)
(/ (- b) a)
(if (<= b 1.35e-40)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.15e-68) {
tmp = -b / a;
} else if (b <= 1.35e-40) {
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.15d-68)) then
tmp = -b / a
else if (b <= 1.35d-40) 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.15e-68) {
tmp = -b / a;
} else if (b <= 1.35e-40) {
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.15e-68: tmp = -b / a elif b <= 1.35e-40: 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.15e-68) tmp = Float64(Float64(-b) / a); elseif (b <= 1.35e-40) 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 <= -1.15e-68) tmp = -b / a; elseif (b <= 1.35e-40) 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.15e-68], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.35e-40], 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.15 \cdot 10^{-68}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.35 \cdot 10^{-40}:\\
\;\;\;\;\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.14999999999999998e-68Initial program 69.8%
*-commutative69.8%
Simplified69.8%
Taylor expanded in b around -inf 88.8%
associate-*r/88.8%
mul-1-neg88.8%
Simplified88.8%
if -1.14999999999999998e-68 < b < 1.35e-40Initial program 75.5%
*-commutative75.5%
Simplified75.5%
prod-diff75.1%
*-commutative75.1%
fma-def75.1%
associate-+l+75.1%
pow275.1%
associate-*l*75.1%
distribute-lft-neg-in75.1%
metadata-eval75.1%
*-commutative75.1%
associate-*r*75.1%
*-commutative75.1%
fma-udef75.1%
distribute-lft-neg-in75.1%
distribute-rgt-neg-in75.1%
distribute-lft-neg-in75.1%
metadata-eval75.1%
associate-*l*75.1%
*-commutative75.1%
*-commutative75.1%
associate-*r*75.1%
Applied egg-rr75.1%
+-commutative75.1%
associate-+r+75.1%
count-275.1%
associate-+l+75.1%
count-275.1%
associate-*r*75.1%
associate-*r*75.0%
distribute-lft-out75.0%
metadata-eval75.0%
+-commutative75.0%
unpow275.0%
fma-udef75.0%
*-commutative75.0%
Simplified75.0%
Taylor expanded in b around 0 65.9%
mul-1-neg65.9%
unsub-neg65.9%
*-commutative65.9%
*-commutative65.9%
associate-*r*65.9%
associate-*r*66.0%
*-commutative66.0%
*-commutative66.0%
distribute-lft-in66.4%
distribute-rgt-out66.4%
metadata-eval66.4%
Simplified66.4%
if 1.35e-40 < b Initial program 15.6%
*-commutative15.6%
Simplified15.6%
Taylor expanded in b around inf 90.0%
mul-1-neg90.0%
distribute-neg-frac90.0%
Simplified90.0%
Final simplification82.7%
(FPCore (a b c) :precision binary64 (if (<= b -9.5e-119) (/ (- b) a) (if (<= b 1.1e-40) (/ (sqrt (* a (* c -4.0))) (* a 2.0)) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -9.5e-119) {
tmp = -b / a;
} else if (b <= 1.1e-40) {
tmp = sqrt((a * (c * -4.0))) / (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 <= (-9.5d-119)) then
tmp = -b / a
else if (b <= 1.1d-40) then
tmp = sqrt((a * (c * (-4.0d0)))) / (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 <= -9.5e-119) {
tmp = -b / a;
} else if (b <= 1.1e-40) {
tmp = Math.sqrt((a * (c * -4.0))) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -9.5e-119: tmp = -b / a elif b <= 1.1e-40: tmp = math.sqrt((a * (c * -4.0))) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -9.5e-119) tmp = Float64(Float64(-b) / a); elseif (b <= 1.1e-40) tmp = Float64(sqrt(Float64(a * Float64(c * -4.0))) / 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 <= -9.5e-119) tmp = -b / a; elseif (b <= 1.1e-40) tmp = sqrt((a * (c * -4.0))) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -9.5e-119], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.1e-40], N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.5 \cdot 10^{-119}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.1 \cdot 10^{-40}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)}}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -9.5000000000000002e-119Initial program 71.9%
*-commutative71.9%
Simplified71.9%
Taylor expanded in b around -inf 84.8%
associate-*r/84.8%
mul-1-neg84.8%
Simplified84.8%
if -9.5000000000000002e-119 < b < 1.10000000000000004e-40Initial program 72.8%
*-commutative72.8%
Simplified72.8%
prod-diff72.4%
*-commutative72.4%
fma-def72.4%
associate-+l+72.4%
pow272.4%
associate-*l*72.3%
distribute-lft-neg-in72.3%
metadata-eval72.3%
*-commutative72.3%
associate-*r*72.4%
*-commutative72.4%
fma-udef72.4%
distribute-lft-neg-in72.4%
distribute-rgt-neg-in72.4%
distribute-lft-neg-in72.4%
metadata-eval72.4%
associate-*l*72.4%
*-commutative72.4%
*-commutative72.4%
associate-*r*72.4%
Applied egg-rr72.4%
+-commutative72.4%
associate-+r+72.4%
count-272.4%
associate-+l+72.4%
count-272.4%
associate-*r*72.3%
associate-*r*72.3%
distribute-lft-out72.3%
metadata-eval72.3%
+-commutative72.3%
unpow272.3%
fma-udef72.3%
*-commutative72.3%
Simplified72.3%
add-sqr-sqrt71.9%
pow271.9%
pow1/271.9%
sqrt-pow172.0%
associate-*l*72.1%
fma-def72.5%
metadata-eval72.5%
Applied egg-rr72.5%
Taylor expanded in b around 0 67.5%
distribute-rgt-out68.0%
metadata-eval68.0%
associate-*r*68.0%
Simplified68.0%
if 1.10000000000000004e-40 < b Initial program 15.6%
*-commutative15.6%
Simplified15.6%
Taylor expanded in b around inf 90.0%
mul-1-neg90.0%
distribute-neg-frac90.0%
Simplified90.0%
Final simplification82.3%
(FPCore (a b c) :precision binary64 (if (<= b -1e-309) (- (/ c b) (/ b a)) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -1e-309) {
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 <= (-1d-309)) 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 <= -1e-309) {
tmp = (c / b) - (b / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1e-309: tmp = (c / b) - (b / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1e-309) 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 <= -1e-309) tmp = (c / b) - (b / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1e-309], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1 \cdot 10^{-309}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.000000000000002e-309Initial program 74.4%
*-commutative74.4%
Simplified74.4%
Taylor expanded in b around -inf 71.0%
+-commutative71.0%
mul-1-neg71.0%
unsub-neg71.0%
Simplified71.0%
if -1.000000000000002e-309 < b Initial program 29.7%
*-commutative29.7%
Simplified29.7%
Taylor expanded in b around inf 70.1%
mul-1-neg70.1%
distribute-neg-frac70.1%
Simplified70.1%
Final simplification70.6%
(FPCore (a b c) :precision binary64 (if (<= b 5.2e-278) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 5.2e-278) {
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 <= 5.2d-278) 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 <= 5.2e-278) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 5.2e-278: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 5.2e-278) 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 <= 5.2e-278) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 5.2e-278], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 5.2 \cdot 10^{-278}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 5.1999999999999997e-278Initial program 74.6%
*-commutative74.6%
Simplified74.6%
Taylor expanded in b around -inf 70.5%
associate-*r/70.5%
mul-1-neg70.5%
Simplified70.5%
if 5.1999999999999997e-278 < b Initial program 29.1%
*-commutative29.1%
Simplified29.1%
Taylor expanded in b around inf 70.7%
mul-1-neg70.7%
distribute-neg-frac70.7%
Simplified70.7%
Final simplification70.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(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 54.3%
*-commutative54.3%
Simplified54.3%
Taylor expanded in b around -inf 40.3%
associate-*r/40.3%
mul-1-neg40.3%
Simplified40.3%
Final simplification40.3%
herbie shell --seed 2023320
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))