
(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 -4e+113)
(- (/ c b) (/ b a))
(if (<= b 9e-12)
(/ (- (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 <= -4e+113) {
tmp = (c / b) - (b / a);
} else if (b <= 9e-12) {
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 <= (-4d+113)) then
tmp = (c / b) - (b / a)
else if (b <= 9d-12) 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 <= -4e+113) {
tmp = (c / b) - (b / a);
} else if (b <= 9e-12) {
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 <= -4e+113: tmp = (c / b) - (b / a) elif b <= 9e-12: 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 <= -4e+113) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 9e-12) 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 <= -4e+113) tmp = (c / b) - (b / a); elseif (b <= 9e-12) 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, -4e+113], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 9e-12], 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 \cdot 10^{+113}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 9 \cdot 10^{-12}:\\
\;\;\;\;\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 < -4e113Initial program 52.6%
*-commutative52.6%
Simplified52.6%
Taylor expanded in b around -inf 90.7%
+-commutative90.7%
mul-1-neg90.7%
unsub-neg90.7%
Applied egg-rr90.7%
if -4e113 < b < 8.99999999999999962e-12Initial program 86.9%
if 8.99999999999999962e-12 < b Initial program 14.6%
*-commutative14.6%
Simplified14.6%
Taylor expanded in b around inf 90.9%
mul-1-neg90.9%
distribute-neg-frac90.9%
Simplified90.9%
Final simplification88.8%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (- (/ c b) (/ b a)))
(t_1 (/ (- (sqrt (* (* c a) -4.0)) b) (* a 2.0))))
(if (<= b -6.6e-15)
t_0
(if (<= b -2.5e-66)
t_1
(if (<= b -4.5e-133) t_0 (if (<= b 3e-12) t_1 (/ (- c) b)))))))
double code(double a, double b, double c) {
double t_0 = (c / b) - (b / a);
double t_1 = (sqrt(((c * a) * -4.0)) - b) / (a * 2.0);
double tmp;
if (b <= -6.6e-15) {
tmp = t_0;
} else if (b <= -2.5e-66) {
tmp = t_1;
} else if (b <= -4.5e-133) {
tmp = t_0;
} else if (b <= 3e-12) {
tmp = t_1;
} 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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = (c / b) - (b / a)
t_1 = (sqrt(((c * a) * (-4.0d0))) - b) / (a * 2.0d0)
if (b <= (-6.6d-15)) then
tmp = t_0
else if (b <= (-2.5d-66)) then
tmp = t_1
else if (b <= (-4.5d-133)) then
tmp = t_0
else if (b <= 3d-12) then
tmp = t_1
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = (c / b) - (b / a);
double t_1 = (Math.sqrt(((c * a) * -4.0)) - b) / (a * 2.0);
double tmp;
if (b <= -6.6e-15) {
tmp = t_0;
} else if (b <= -2.5e-66) {
tmp = t_1;
} else if (b <= -4.5e-133) {
tmp = t_0;
} else if (b <= 3e-12) {
tmp = t_1;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): t_0 = (c / b) - (b / a) t_1 = (math.sqrt(((c * a) * -4.0)) - b) / (a * 2.0) tmp = 0 if b <= -6.6e-15: tmp = t_0 elif b <= -2.5e-66: tmp = t_1 elif b <= -4.5e-133: tmp = t_0 elif b <= 3e-12: tmp = t_1 else: tmp = -c / b return tmp
function code(a, b, c) t_0 = Float64(Float64(c / b) - Float64(b / a)) t_1 = Float64(Float64(sqrt(Float64(Float64(c * a) * -4.0)) - b) / Float64(a * 2.0)) tmp = 0.0 if (b <= -6.6e-15) tmp = t_0; elseif (b <= -2.5e-66) tmp = t_1; elseif (b <= -4.5e-133) tmp = t_0; elseif (b <= 3e-12) tmp = t_1; else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) t_0 = (c / b) - (b / a); t_1 = (sqrt(((c * a) * -4.0)) - b) / (a * 2.0); tmp = 0.0; if (b <= -6.6e-15) tmp = t_0; elseif (b <= -2.5e-66) tmp = t_1; elseif (b <= -4.5e-133) tmp = t_0; elseif (b <= 3e-12) tmp = t_1; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[Sqrt[N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -6.6e-15], t$95$0, If[LessEqual[b, -2.5e-66], t$95$1, If[LessEqual[b, -4.5e-133], t$95$0, If[LessEqual[b, 3e-12], t$95$1, N[((-c) / b), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{c}{b} - \frac{b}{a}\\
t_1 := \frac{\sqrt{\left(c \cdot a\right) \cdot -4} - b}{a \cdot 2}\\
\mathbf{if}\;b \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq -2.5 \cdot 10^{-66}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq -4.5 \cdot 10^{-133}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 3 \cdot 10^{-12}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -6.6e-15 or -2.49999999999999981e-66 < b < -4.50000000000000009e-133Initial program 73.4%
*-commutative73.4%
Simplified73.4%
Taylor expanded in b around -inf 86.1%
+-commutative86.1%
mul-1-neg86.1%
unsub-neg86.1%
Applied egg-rr86.1%
if -6.6e-15 < b < -2.49999999999999981e-66 or -4.50000000000000009e-133 < b < 3.0000000000000001e-12Initial program 81.6%
*-commutative81.6%
Simplified81.6%
Taylor expanded in b around 0 78.8%
*-commutative78.8%
Simplified78.8%
if 3.0000000000000001e-12 < b Initial program 14.6%
*-commutative14.6%
Simplified14.6%
Taylor expanded in b around inf 90.9%
mul-1-neg90.9%
distribute-neg-frac90.9%
Simplified90.9%
Final simplification85.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 77.4%
*-commutative77.4%
Simplified77.4%
Taylor expanded in b around -inf 69.3%
+-commutative69.3%
mul-1-neg69.3%
unsub-neg69.3%
Applied egg-rr69.3%
if -4.999999999999985e-310 < b Initial program 39.8%
*-commutative39.8%
Simplified39.8%
Taylor expanded in b around inf 60.9%
mul-1-neg60.9%
distribute-neg-frac60.9%
Simplified60.9%
Final simplification65.6%
(FPCore (a b c) :precision binary64 (if (<= b 3.15e-7) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.15e-7) {
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.15d-7) 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.15e-7) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.15e-7: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.15e-7) tmp = Float64(Float64(-b) / a); else tmp = Float64(c / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 3.15e-7) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.15e-7], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.15 \cdot 10^{-7}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 3.15000000000000002e-7Initial program 76.8%
*-commutative76.8%
Simplified76.8%
Taylor expanded in b around -inf 52.8%
associate-*r/52.8%
mul-1-neg52.8%
Simplified52.8%
if 3.15000000000000002e-7 < b Initial program 14.6%
*-commutative14.6%
Simplified14.6%
Taylor expanded in b around -inf 2.5%
Taylor expanded in b around 0 25.6%
Final simplification45.8%
(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 77.4%
*-commutative77.4%
Simplified77.4%
Taylor expanded in b around -inf 69.2%
associate-*r/69.2%
mul-1-neg69.2%
Simplified69.2%
if -4.999999999999985e-310 < b Initial program 39.8%
*-commutative39.8%
Simplified39.8%
Taylor expanded in b around inf 60.9%
mul-1-neg60.9%
distribute-neg-frac60.9%
Simplified60.9%
Final simplification65.5%
(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 60.8%
*-commutative60.8%
Simplified60.8%
Taylor expanded in b around -inf 39.6%
associate-*r/39.6%
neg-mul-139.6%
clear-num39.6%
metadata-eval39.6%
frac-add25.0%
add-sqr-sqrt24.3%
sqrt-unprod23.1%
sqr-neg23.1%
unpow223.1%
unpow223.1%
sqrt-prod0.7%
add-sqr-sqrt1.9%
*-commutative1.9%
metadata-eval1.9%
*-un-lft-identity1.9%
Applied egg-rr1.9%
Taylor expanded in b around inf 2.4%
Final simplification2.4%
(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 60.8%
*-commutative60.8%
Simplified60.8%
Taylor expanded in b around -inf 39.6%
Taylor expanded in b around 0 8.8%
Final simplification8.8%
herbie shell --seed 2023305
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))