
(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 -24000000.0)
(- (/ c b) (/ b a))
(if (<= b 1.12e-60)
(/ (- (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 <= -24000000.0) {
tmp = (c / b) - (b / a);
} else if (b <= 1.12e-60) {
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 <= (-24000000.0d0)) then
tmp = (c / b) - (b / a)
else if (b <= 1.12d-60) 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 <= -24000000.0) {
tmp = (c / b) - (b / a);
} else if (b <= 1.12e-60) {
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 <= -24000000.0: tmp = (c / b) - (b / a) elif b <= 1.12e-60: 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 <= -24000000.0) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 1.12e-60) 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 <= -24000000.0) tmp = (c / b) - (b / a); elseif (b <= 1.12e-60) 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, -24000000.0], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.12e-60], 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 -24000000:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 1.12 \cdot 10^{-60}:\\
\;\;\;\;\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.4e7Initial program 64.8%
*-commutative64.8%
Simplified64.8%
Taylor expanded in b around -inf 94.5%
+-commutative94.5%
mul-1-neg94.5%
unsub-neg94.5%
Simplified94.5%
if -2.4e7 < b < 1.12e-60Initial program 72.8%
if 1.12e-60 < b Initial program 21.0%
*-commutative21.0%
Simplified21.0%
Taylor expanded in b around inf 87.2%
mul-1-neg87.2%
distribute-neg-frac87.2%
Simplified87.2%
Final simplification83.7%
(FPCore (a b c)
:precision binary64
(if (<= b -4.6e-54)
(- (/ c b) (/ b a))
(if (<= b 1.45e-60)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -4.6e-54) {
tmp = (c / b) - (b / a);
} else if (b <= 1.45e-60) {
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 <= (-4.6d-54)) then
tmp = (c / b) - (b / a)
else if (b <= 1.45d-60) 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 <= -4.6e-54) {
tmp = (c / b) - (b / a);
} else if (b <= 1.45e-60) {
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 <= -4.6e-54: tmp = (c / b) - (b / a) elif b <= 1.45e-60: 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 <= -4.6e-54) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 1.45e-60) 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 <= -4.6e-54) tmp = (c / b) - (b / a); elseif (b <= 1.45e-60) 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, -4.6e-54], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.45e-60], 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 -4.6 \cdot 10^{-54}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 1.45 \cdot 10^{-60}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -4.5999999999999998e-54Initial program 70.7%
*-commutative70.7%
Simplified70.7%
Taylor expanded in b around -inf 92.2%
+-commutative92.2%
mul-1-neg92.2%
unsub-neg92.2%
Simplified92.2%
if -4.5999999999999998e-54 < b < 1.45e-60Initial program 68.3%
*-commutative68.3%
Simplified68.3%
prod-diff67.7%
*-commutative67.7%
fma-def67.7%
associate-+l+67.7%
pow267.7%
distribute-lft-neg-in67.7%
*-commutative67.7%
distribute-rgt-neg-in67.7%
metadata-eval67.7%
associate-*r*67.7%
*-commutative67.7%
*-commutative67.7%
fma-udef67.7%
Applied egg-rr67.7%
fma-def67.7%
fma-def67.7%
associate-*l*67.7%
Simplified67.7%
Taylor expanded in b around 0 62.1%
distribute-rgt-out63.9%
metadata-eval63.9%
associate-*r*63.9%
mul-1-neg63.9%
unsub-neg63.9%
*-commutative63.9%
Simplified63.9%
if 1.45e-60 < b Initial program 21.0%
*-commutative21.0%
Simplified21.0%
Taylor expanded in b around inf 87.2%
mul-1-neg87.2%
distribute-neg-frac87.2%
Simplified87.2%
Final simplification81.6%
(FPCore (a b c) :precision binary64 (if (<= b -1.05e-54) (- (/ c b) (/ b a)) (if (<= b 7e-61) (* 0.5 (/ (sqrt (* a (* c -4.0))) a)) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.05e-54) {
tmp = (c / b) - (b / a);
} else if (b <= 7e-61) {
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 <= (-1.05d-54)) then
tmp = (c / b) - (b / a)
else if (b <= 7d-61) 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 <= -1.05e-54) {
tmp = (c / b) - (b / a);
} else if (b <= 7e-61) {
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 <= -1.05e-54: tmp = (c / b) - (b / a) elif b <= 7e-61: 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 <= -1.05e-54) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 7e-61) 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 <= -1.05e-54) tmp = (c / b) - (b / a); elseif (b <= 7e-61) 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, -1.05e-54], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 7e-61], 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 -1.05 \cdot 10^{-54}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 7 \cdot 10^{-61}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{a \cdot \left(c \cdot -4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.05e-54Initial program 70.7%
*-commutative70.7%
Simplified70.7%
Taylor expanded in b around -inf 92.2%
+-commutative92.2%
mul-1-neg92.2%
unsub-neg92.2%
Simplified92.2%
if -1.05e-54 < b < 7.0000000000000006e-61Initial program 68.3%
*-commutative68.3%
Simplified68.3%
prod-diff67.7%
*-commutative67.7%
fma-def67.7%
associate-+l+67.7%
pow267.7%
distribute-lft-neg-in67.7%
*-commutative67.7%
distribute-rgt-neg-in67.7%
metadata-eval67.7%
associate-*r*67.7%
*-commutative67.7%
*-commutative67.7%
fma-udef67.7%
Applied egg-rr67.7%
fma-def67.7%
fma-def67.7%
associate-*l*67.7%
Simplified67.7%
Taylor expanded in b around 0 61.0%
associate-*l/61.0%
distribute-rgt-out62.7%
metadata-eval62.7%
associate-*r*62.7%
*-lft-identity62.7%
*-commutative62.7%
Simplified62.7%
if 7.0000000000000006e-61 < b Initial program 21.0%
*-commutative21.0%
Simplified21.0%
Taylor expanded in b around inf 87.2%
mul-1-neg87.2%
distribute-neg-frac87.2%
Simplified87.2%
Final simplification81.2%
(FPCore (a b c) :precision binary64 (if (<= b -7.4e-300) (/ (- b) a) 0.0))
double code(double a, double b, double c) {
double tmp;
if (b <= -7.4e-300) {
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 <= (-7.4d-300)) 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 <= -7.4e-300) {
tmp = -b / a;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -7.4e-300: tmp = -b / a else: tmp = 0.0 return tmp
function code(a, b, c) tmp = 0.0 if (b <= -7.4e-300) 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 <= -7.4e-300) tmp = -b / a; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -7.4e-300], N[((-b) / a), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -7.4 \cdot 10^{-300}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < -7.4000000000000003e-300Initial program 72.4%
*-commutative72.4%
Simplified72.4%
Taylor expanded in b around -inf 68.1%
associate-*r/68.1%
mul-1-neg68.1%
Simplified68.1%
if -7.4000000000000003e-300 < b Initial program 33.1%
*-commutative33.1%
Simplified33.1%
Applied egg-rr30.9%
fma-def26.0%
distribute-neg-frac26.0%
Simplified26.0%
Taylor expanded in a around 0 20.1%
distribute-rgt-out20.1%
metadata-eval20.1%
associate-*l/15.2%
mul0-rgt20.1%
Simplified20.1%
Final simplification43.6%
(FPCore (a b c) :precision binary64 (if (<= b -5e-310) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-310) {
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 <= (-5d-310)) 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 <= -5e-310) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-310: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-310) 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 <= -5e-310) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -4.999999999999985e-310Initial program 72.8%
*-commutative72.8%
Simplified72.8%
Taylor expanded in b around -inf 67.1%
associate-*r/67.1%
mul-1-neg67.1%
Simplified67.1%
if -4.999999999999985e-310 < b Initial program 32.0%
*-commutative32.0%
Simplified32.0%
Taylor expanded in b around inf 66.3%
mul-1-neg66.3%
distribute-neg-frac66.3%
Simplified66.3%
Final simplification66.7%
(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 52.3%
*-commutative52.3%
Simplified52.3%
Applied egg-rr51.1%
fma-def48.7%
distribute-neg-frac48.7%
Simplified48.7%
Taylor expanded in a around 0 11.7%
distribute-rgt-out11.7%
metadata-eval11.7%
associate-*l/9.0%
mul0-rgt11.7%
Simplified11.7%
Final simplification11.7%
herbie shell --seed 2024031
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))