
(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+154)
(- (/ c b) (/ b a))
(if (<= b 2.15e-108)
(/ (- (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 <= -2e+154) {
tmp = (c / b) - (b / a);
} else if (b <= 2.15e-108) {
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 <= (-2d+154)) then
tmp = (c / b) - (b / a)
else if (b <= 2.15d-108) 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 <= -2e+154) {
tmp = (c / b) - (b / a);
} else if (b <= 2.15e-108) {
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 <= -2e+154: tmp = (c / b) - (b / a) elif b <= 2.15e-108: 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 <= -2e+154) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 2.15e-108) 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 <= -2e+154) tmp = (c / b) - (b / a); elseif (b <= 2.15e-108) 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, -2e+154], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2.15e-108], 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 -2 \cdot 10^{+154}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 2.15 \cdot 10^{-108}:\\
\;\;\;\;\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 < -2.00000000000000007e154Initial program 41.2%
neg-sub041.2%
associate-+l-41.2%
sub0-neg41.2%
neg-mul-141.2%
associate-*l/41.2%
*-commutative41.2%
associate-/r*41.2%
/-rgt-identity41.2%
metadata-eval41.2%
Simplified41.3%
Taylor expanded in b around -inf 96.1%
mul-1-neg96.1%
unsub-neg96.1%
Simplified96.1%
if -2.00000000000000007e154 < b < 2.15e-108Initial program 88.0%
if 2.15e-108 < b Initial program 15.9%
neg-sub015.9%
associate-+l-15.9%
sub0-neg15.9%
neg-mul-115.9%
associate-*l/15.9%
*-commutative15.9%
associate-/r*15.9%
/-rgt-identity15.9%
metadata-eval15.9%
Simplified15.9%
Taylor expanded in b around inf 87.6%
mul-1-neg87.6%
distribute-neg-frac87.6%
Simplified87.6%
Final simplification89.3%
(FPCore (a b c)
:precision binary64
(if (<= b -1.45e-23)
(- (/ c b) (/ b a))
(if (<= b 2.2e-108)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.45e-23) {
tmp = (c / b) - (b / a);
} else if (b <= 2.2e-108) {
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.45d-23)) then
tmp = (c / b) - (b / a)
else if (b <= 2.2d-108) 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.45e-23) {
tmp = (c / b) - (b / a);
} else if (b <= 2.2e-108) {
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.45e-23: tmp = (c / b) - (b / a) elif b <= 2.2e-108: 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.45e-23) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 2.2e-108) 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.45e-23) tmp = (c / b) - (b / a); elseif (b <= 2.2e-108) 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.45e-23], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2.2e-108], 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.45 \cdot 10^{-23}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 2.2 \cdot 10^{-108}:\\
\;\;\;\;\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.4500000000000001e-23Initial program 69.4%
neg-sub069.4%
associate-+l-69.4%
sub0-neg69.4%
neg-mul-169.4%
associate-*l/69.3%
*-commutative69.3%
associate-/r*69.3%
/-rgt-identity69.3%
metadata-eval69.3%
Simplified69.3%
Taylor expanded in b around -inf 88.9%
mul-1-neg88.9%
unsub-neg88.9%
Simplified88.9%
if -1.4500000000000001e-23 < b < 2.2000000000000001e-108Initial program 83.4%
Taylor expanded in b around 0 75.7%
*-commutative75.7%
*-commutative75.7%
associate-*r*75.7%
Simplified75.7%
if 2.2000000000000001e-108 < b Initial program 15.9%
neg-sub015.9%
associate-+l-15.9%
sub0-neg15.9%
neg-mul-115.9%
associate-*l/15.9%
*-commutative15.9%
associate-/r*15.9%
/-rgt-identity15.9%
metadata-eval15.9%
Simplified15.9%
Taylor expanded in b around inf 87.6%
mul-1-neg87.6%
distribute-neg-frac87.6%
Simplified87.6%
Final simplification84.6%
(FPCore (a b c) :precision binary64 (if (<= b -4e-310) (- (/ c b) (/ b a)) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -4e-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 <= (-4d-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 <= -4e-310) {
tmp = (c / b) - (b / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -4e-310: tmp = (c / b) - (b / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -4e-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 <= -4e-310) tmp = (c / b) - (b / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -4e-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 -4 \cdot 10^{-310}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -3.999999999999988e-310Initial program 72.6%
neg-sub072.6%
associate-+l-72.6%
sub0-neg72.6%
neg-mul-172.6%
associate-*l/72.5%
*-commutative72.5%
associate-/r*72.5%
/-rgt-identity72.5%
metadata-eval72.5%
Simplified72.5%
Taylor expanded in b around -inf 69.7%
mul-1-neg69.7%
unsub-neg69.7%
Simplified69.7%
if -3.999999999999988e-310 < b Initial program 36.6%
neg-sub036.6%
associate-+l-36.6%
sub0-neg36.6%
neg-mul-136.6%
associate-*l/36.6%
*-commutative36.6%
associate-/r*36.6%
/-rgt-identity36.6%
metadata-eval36.6%
Simplified36.6%
Taylor expanded in b around inf 65.5%
mul-1-neg65.5%
distribute-neg-frac65.5%
Simplified65.5%
Final simplification67.8%
(FPCore (a b c) :precision binary64 (if (<= b -4e-310) (/ (- b) a) 0.0))
double code(double a, double b, double c) {
double tmp;
if (b <= -4e-310) {
tmp = -b / a;
} else {
tmp = 0.0;
}
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-310)) then
tmp = -b / a
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -4e-310) {
tmp = -b / a;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -4e-310: tmp = -b / a else: tmp = 0.0 return tmp
function code(a, b, c) tmp = 0.0 if (b <= -4e-310) tmp = Float64(Float64(-b) / a); else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -4e-310) tmp = -b / a; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -4e-310], N[((-b) / a), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -3.999999999999988e-310Initial program 72.6%
neg-sub072.6%
associate-+l-72.6%
sub0-neg72.6%
neg-mul-172.6%
associate-*l/72.5%
*-commutative72.5%
associate-/r*72.5%
/-rgt-identity72.5%
metadata-eval72.5%
Simplified72.5%
Taylor expanded in b around -inf 69.3%
associate-*r/69.3%
mul-1-neg69.3%
Simplified69.3%
if -3.999999999999988e-310 < b Initial program 36.6%
neg-sub036.6%
associate-+l-36.6%
sub0-neg36.6%
neg-mul-136.6%
associate-*l/36.6%
*-commutative36.6%
associate-/r*36.6%
/-rgt-identity36.6%
metadata-eval36.6%
Simplified36.6%
*-commutative36.6%
sub-neg36.6%
distribute-lft-in36.3%
Applied egg-rr36.3%
Taylor expanded in a around 0 16.6%
distribute-rgt-out16.6%
metadata-eval16.6%
associate-*l/10.8%
mul0-rgt16.6%
Simplified16.6%
Final simplification45.8%
(FPCore (a b c) :precision binary64 (if (<= b 3.8e-305) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.8e-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 <= 3.8d-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 <= 3.8e-305) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.8e-305: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.8e-305) 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.8e-305) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.8e-305], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.8 \cdot 10^{-305}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 3.8e-305Initial program 72.8%
neg-sub072.8%
associate-+l-72.8%
sub0-neg72.8%
neg-mul-172.8%
associate-*l/72.7%
*-commutative72.7%
associate-/r*72.7%
/-rgt-identity72.7%
metadata-eval72.7%
Simplified72.7%
Taylor expanded in b around -inf 68.9%
associate-*r/68.9%
mul-1-neg68.9%
Simplified68.9%
if 3.8e-305 < b Initial program 36.0%
neg-sub036.0%
associate-+l-36.0%
sub0-neg36.0%
neg-mul-136.0%
associate-*l/36.0%
*-commutative36.0%
associate-/r*36.0%
/-rgt-identity36.0%
metadata-eval36.0%
Simplified36.1%
Taylor expanded in b around inf 66.1%
mul-1-neg66.1%
distribute-neg-frac66.1%
Simplified66.1%
Final simplification67.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 56.6%
neg-sub056.6%
associate-+l-56.6%
sub0-neg56.6%
neg-mul-156.6%
associate-*l/56.5%
*-commutative56.5%
associate-/r*56.5%
/-rgt-identity56.5%
metadata-eval56.5%
Simplified56.5%
Taylor expanded in b around -inf 39.6%
mul-1-neg39.6%
unsub-neg39.6%
Simplified39.6%
Taylor expanded in c around inf 8.9%
Final simplification8.9%
(FPCore (a b c) :precision binary64 0.0)
double code(double a, double b, double c) {
return 0.0;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 0.0d0
end function
public static double code(double a, double b, double c) {
return 0.0;
}
def code(a, b, c): return 0.0
function code(a, b, c) return 0.0 end
function tmp = code(a, b, c) tmp = 0.0; end
code[a_, b_, c_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 56.6%
neg-sub056.6%
associate-+l-56.6%
sub0-neg56.6%
neg-mul-156.6%
associate-*l/56.5%
*-commutative56.5%
associate-/r*56.5%
/-rgt-identity56.5%
metadata-eval56.5%
Simplified56.5%
*-commutative56.5%
sub-neg56.5%
distribute-lft-in56.4%
Applied egg-rr56.4%
Taylor expanded in a around 0 8.8%
distribute-rgt-out8.8%
metadata-eval8.8%
associate-*l/6.0%
mul0-rgt8.8%
Simplified8.8%
Final simplification8.8%
herbie shell --seed 2023242
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))