
(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 (+ (* (* 2.0 x) eps) (* eps eps)))
double code(double x, double eps) {
return ((2.0 * x) * eps) + (eps * eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((2.0d0 * x) * eps) + (eps * eps)
end function
public static double code(double x, double eps) {
return ((2.0 * x) * eps) + (eps * eps);
}
def code(x, eps): return ((2.0 * x) * eps) + (eps * eps)
function code(x, eps) return Float64(Float64(Float64(2.0 * x) * eps) + Float64(eps * eps)) end
function tmp = code(x, eps) tmp = ((2.0 * x) * eps) + (eps * eps); end
code[x_, eps_] := N[(N[(N[(2.0 * x), $MachinePrecision] * eps), $MachinePrecision] + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(2 \cdot x\right) \cdot \varepsilon + \varepsilon \cdot \varepsilon
\end{array}
Initial program 74.5%
unpow274.5%
unpow274.5%
difference-of-squares74.5%
*-commutative74.5%
+-commutative74.5%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
fma-udef100.0%
distribute-rgt-in100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -8.5e-92) (not (<= x 7.2e-94))) (* 2.0 (* x eps)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -8.5e-92) || !(x <= 7.2e-94)) {
tmp = 2.0 * (x * eps);
} 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 <= (-8.5d-92)) .or. (.not. (x <= 7.2d-94))) then
tmp = 2.0d0 * (x * eps)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -8.5e-92) || !(x <= 7.2e-94)) {
tmp = 2.0 * (x * eps);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -8.5e-92) or not (x <= 7.2e-94): tmp = 2.0 * (x * eps) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -8.5e-92) || !(x <= 7.2e-94)) tmp = Float64(2.0 * Float64(x * eps)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -8.5e-92) || ~((x <= 7.2e-94))) tmp = 2.0 * (x * eps); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -8.5e-92], N[Not[LessEqual[x, 7.2e-94]], $MachinePrecision]], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.5 \cdot 10^{-92} \lor \neg \left(x \leq 7.2 \cdot 10^{-94}\right):\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -8.50000000000000067e-92 or 7.2e-94 < x Initial program 28.2%
unpow228.2%
unpow228.2%
difference-of-squares28.2%
*-commutative28.2%
+-commutative28.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around 0 90.3%
if -8.50000000000000067e-92 < x < 7.2e-94Initial program 96.0%
unpow296.0%
unpow296.0%
difference-of-squares96.0%
*-commutative96.0%
+-commutative96.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around inf 94.1%
unpow294.1%
Simplified94.1%
Final simplification92.9%
(FPCore (x eps) :precision binary64 (if (<= x -9.5e-92) (* 2.0 (* x eps)) (if (<= x 3.4e-93) (* eps eps) (* eps (+ x x)))))
double code(double x, double eps) {
double tmp;
if (x <= -9.5e-92) {
tmp = 2.0 * (x * eps);
} else if (x <= 3.4e-93) {
tmp = eps * eps;
} else {
tmp = eps * (x + x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-9.5d-92)) then
tmp = 2.0d0 * (x * eps)
else if (x <= 3.4d-93) then
tmp = eps * eps
else
tmp = eps * (x + x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -9.5e-92) {
tmp = 2.0 * (x * eps);
} else if (x <= 3.4e-93) {
tmp = eps * eps;
} else {
tmp = eps * (x + x);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -9.5e-92: tmp = 2.0 * (x * eps) elif x <= 3.4e-93: tmp = eps * eps else: tmp = eps * (x + x) return tmp
function code(x, eps) tmp = 0.0 if (x <= -9.5e-92) tmp = Float64(2.0 * Float64(x * eps)); elseif (x <= 3.4e-93) tmp = Float64(eps * eps); else tmp = Float64(eps * Float64(x + x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -9.5e-92) tmp = 2.0 * (x * eps); elseif (x <= 3.4e-93) tmp = eps * eps; else tmp = eps * (x + x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -9.5e-92], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 3.4e-93], N[(eps * eps), $MachinePrecision], N[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.5 \cdot 10^{-92}:\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\mathbf{elif}\;x \leq 3.4 \cdot 10^{-93}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(x + x\right)\\
\end{array}
\end{array}
if x < -9.49999999999999946e-92Initial program 33.1%
unpow233.1%
unpow233.1%
difference-of-squares33.1%
*-commutative33.1%
+-commutative33.1%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around 0 89.5%
if -9.49999999999999946e-92 < x < 3.40000000000000001e-93Initial program 96.0%
unpow296.0%
unpow296.0%
difference-of-squares96.0%
*-commutative96.0%
+-commutative96.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around inf 94.1%
unpow294.1%
Simplified94.1%
if 3.40000000000000001e-93 < x Initial program 21.0%
unpow221.0%
unpow221.0%
difference-of-squares21.0%
*-commutative21.0%
+-commutative21.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
fma-udef100.0%
distribute-rgt-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 91.3%
count-291.3%
distribute-lft-out91.4%
Simplified91.4%
Final simplification92.9%
(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 74.5%
unpow274.5%
unpow274.5%
difference-of-squares74.5%
*-commutative74.5%
+-commutative74.5%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around inf 72.5%
unpow272.5%
Simplified72.5%
Final simplification72.5%
herbie shell --seed 2023195
(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)))