
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 2.0) (pow x 2.0)))
double code(double x, double eps) {
return pow((x + eps), 2.0) - pow(x, 2.0);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((x + eps) ** 2.0d0) - (x ** 2.0d0)
end function
public static double code(double x, double eps) {
return Math.pow((x + eps), 2.0) - Math.pow(x, 2.0);
}
def code(x, eps): return math.pow((x + eps), 2.0) - math.pow(x, 2.0)
function code(x, eps) return Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) end
function tmp = code(x, eps) tmp = ((x + eps) ^ 2.0) - (x ^ 2.0); end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 2.0) (pow x 2.0)))
double code(double x, double eps) {
return pow((x + eps), 2.0) - pow(x, 2.0);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((x + eps) ** 2.0d0) - (x ** 2.0d0)
end function
public static double code(double x, double eps) {
return Math.pow((x + eps), 2.0) - Math.pow(x, 2.0);
}
def code(x, eps): return math.pow((x + eps), 2.0) - math.pow(x, 2.0)
function code(x, eps) return Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) end
function tmp = code(x, eps) tmp = ((x + eps) ^ 2.0) - (x ^ 2.0); end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
\end{array}
(FPCore (x eps) :precision binary64 (- (* (/ eps -1.0) (* 2.0 (- x))) (* eps (/ eps -1.0))))
double code(double x, double eps) {
return ((eps / -1.0) * (2.0 * -x)) - (eps * (eps / -1.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((eps / (-1.0d0)) * (2.0d0 * -x)) - (eps * (eps / (-1.0d0)))
end function
public static double code(double x, double eps) {
return ((eps / -1.0) * (2.0 * -x)) - (eps * (eps / -1.0));
}
def code(x, eps): return ((eps / -1.0) * (2.0 * -x)) - (eps * (eps / -1.0))
function code(x, eps) return Float64(Float64(Float64(eps / -1.0) * Float64(2.0 * Float64(-x))) - Float64(eps * Float64(eps / -1.0))) end
function tmp = code(x, eps) tmp = ((eps / -1.0) * (2.0 * -x)) - (eps * (eps / -1.0)); end
code[x_, eps_] := N[(N[(N[(eps / -1.0), $MachinePrecision] * N[(2.0 * (-x)), $MachinePrecision]), $MachinePrecision] - N[(eps * N[(eps / -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{-1} \cdot \left(2 \cdot \left(-x\right)\right) - \varepsilon \cdot \frac{\varepsilon}{-1}
\end{array}
Initial program 75.2%
+-commutative75.2%
unpow275.2%
unpow275.2%
difference-of-squares75.2%
*-commutative75.2%
sub-neg75.2%
+-commutative75.2%
associate-+l+75.2%
remove-double-neg75.2%
sub-neg75.2%
+-commutative75.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
remove-double-neg100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
associate--l-100.0%
neg-mul-1100.0%
neg-mul-1100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
Simplified100.0%
flip3--67.5%
associate-*r/54.4%
associate-/l*67.4%
clear-num67.4%
flip3--99.6%
sub-neg99.6%
distribute-rgt-neg-in99.6%
metadata-eval99.6%
Applied egg-rr99.6%
frac-2neg99.6%
associate-/r/100.0%
+-commutative100.0%
distribute-neg-in100.0%
distribute-lft-in100.0%
metadata-eval100.0%
*-commutative100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -6.2e-83) (not (<= x 1.55e-77))) (* eps (* 2.0 x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -6.2e-83) || !(x <= 1.55e-77)) {
tmp = eps * (2.0 * x);
} else {
tmp = eps * eps;
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((x <= (-6.2d-83)) .or. (.not. (x <= 1.55d-77))) then
tmp = eps * (2.0d0 * x)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -6.2e-83) || !(x <= 1.55e-77)) {
tmp = eps * (2.0 * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -6.2e-83) or not (x <= 1.55e-77): tmp = eps * (2.0 * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -6.2e-83) || !(x <= 1.55e-77)) tmp = Float64(eps * Float64(2.0 * x)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -6.2e-83) || ~((x <= 1.55e-77))) tmp = eps * (2.0 * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -6.2e-83], N[Not[LessEqual[x, 1.55e-77]], $MachinePrecision]], N[(eps * N[(2.0 * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.2 \cdot 10^{-83} \lor \neg \left(x \leq 1.55 \cdot 10^{-77}\right):\\
\;\;\;\;\varepsilon \cdot \left(2 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -6.19999999999999985e-83 or 1.55000000000000004e-77 < x Initial program 26.9%
+-commutative26.9%
unpow226.9%
unpow226.9%
difference-of-squares26.9%
*-commutative26.9%
sub-neg26.9%
+-commutative26.9%
associate-+l+26.9%
remove-double-neg26.9%
sub-neg26.9%
+-commutative26.9%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
remove-double-neg99.9%
sub-neg99.9%
remove-double-neg99.9%
sub-neg99.9%
associate--l-99.9%
neg-mul-199.9%
neg-mul-199.9%
distribute-rgt-out99.9%
metadata-eval99.9%
Simplified99.9%
Taylor expanded in eps around 0 88.6%
*-commutative88.6%
associate-*r*88.6%
*-commutative88.6%
Simplified88.6%
if -6.19999999999999985e-83 < x < 1.55000000000000004e-77Initial program 97.1%
+-commutative97.1%
unpow297.1%
unpow297.1%
difference-of-squares97.1%
*-commutative97.1%
sub-neg97.1%
+-commutative97.1%
associate-+l+97.1%
remove-double-neg97.1%
sub-neg97.1%
+-commutative97.1%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
remove-double-neg100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
associate--l-100.0%
neg-mul-1100.0%
neg-mul-1100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
Simplified100.0%
flip3--53.0%
associate-*r/44.1%
associate-/l*52.8%
clear-num52.9%
flip3--99.7%
sub-neg99.7%
distribute-rgt-neg-in99.7%
metadata-eval99.7%
Applied egg-rr99.7%
Taylor expanded in eps around inf 94.5%
div-inv94.7%
associate-/r/94.8%
metadata-eval94.8%
*-un-lft-identity94.8%
Applied egg-rr94.8%
Final simplification92.8%
(FPCore (x eps) :precision binary64 (* eps (- eps (* x -2.0))))
double code(double x, double eps) {
return eps * (eps - (x * -2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * (eps - (x * (-2.0d0)))
end function
public static double code(double x, double eps) {
return eps * (eps - (x * -2.0));
}
def code(x, eps): return eps * (eps - (x * -2.0))
function code(x, eps) return Float64(eps * Float64(eps - Float64(x * -2.0))) end
function tmp = code(x, eps) tmp = eps * (eps - (x * -2.0)); end
code[x_, eps_] := N[(eps * N[(eps - N[(x * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(\varepsilon - x \cdot -2\right)
\end{array}
Initial program 75.2%
+-commutative75.2%
unpow275.2%
unpow275.2%
difference-of-squares75.2%
*-commutative75.2%
sub-neg75.2%
+-commutative75.2%
associate-+l+75.2%
remove-double-neg75.2%
sub-neg75.2%
+-commutative75.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
remove-double-neg100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
associate--l-100.0%
neg-mul-1100.0%
neg-mul-1100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (* eps eps))
double code(double x, double eps) {
return eps * eps;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * eps
end function
public static double code(double x, double eps) {
return eps * eps;
}
def code(x, eps): return eps * eps
function code(x, eps) return Float64(eps * eps) end
function tmp = code(x, eps) tmp = eps * eps; end
code[x_, eps_] := N[(eps * eps), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \varepsilon
\end{array}
Initial program 75.2%
+-commutative75.2%
unpow275.2%
unpow275.2%
difference-of-squares75.2%
*-commutative75.2%
sub-neg75.2%
+-commutative75.2%
associate-+l+75.2%
remove-double-neg75.2%
sub-neg75.2%
+-commutative75.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
remove-double-neg100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
associate--l-100.0%
neg-mul-1100.0%
neg-mul-1100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
Simplified100.0%
flip3--67.5%
associate-*r/54.4%
associate-/l*67.4%
clear-num67.4%
flip3--99.6%
sub-neg99.6%
distribute-rgt-neg-in99.6%
metadata-eval99.6%
Applied egg-rr99.6%
Taylor expanded in eps around inf 72.2%
div-inv72.3%
associate-/r/72.4%
metadata-eval72.4%
*-un-lft-identity72.4%
Applied egg-rr72.4%
Final simplification72.4%
herbie shell --seed 2023301
(FPCore (x eps)
:name "ENA, Section 1.4, Exercise 4b, n=2"
:precision binary64
:pre (and (and (<= -1000000000.0 x) (<= x 1000000000.0)) (and (<= -1.0 eps) (<= eps 1.0)))
(- (pow (+ x eps) 2.0) (pow x 2.0)))