
(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 5 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 (fma (* 2.0 x) eps (* eps eps)))
double code(double x, double eps) {
return fma((2.0 * x), eps, (eps * eps));
}
function code(x, eps) return fma(Float64(2.0 * x), eps, Float64(eps * eps)) end
code[x_, eps_] := N[(N[(2.0 * x), $MachinePrecision] * eps + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right)
\end{array}
Initial program 79.7%
unpow279.7%
unpow279.7%
difference-of-squares79.7%
*-commutative79.7%
+-commutative79.7%
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%
fma-def100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -2.3e-83) (not (<= x 2.5e-38))) (* 2.0 (* x eps)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -2.3e-83) || !(x <= 2.5e-38)) {
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 <= (-2.3d-83)) .or. (.not. (x <= 2.5d-38))) 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 <= -2.3e-83) || !(x <= 2.5e-38)) {
tmp = 2.0 * (x * eps);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -2.3e-83) or not (x <= 2.5e-38): tmp = 2.0 * (x * eps) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -2.3e-83) || !(x <= 2.5e-38)) 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 <= -2.3e-83) || ~((x <= 2.5e-38))) tmp = 2.0 * (x * eps); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -2.3e-83], N[Not[LessEqual[x, 2.5e-38]], $MachinePrecision]], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.3 \cdot 10^{-83} \lor \neg \left(x \leq 2.5 \cdot 10^{-38}\right):\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -2.2999999999999999e-83 or 2.50000000000000017e-38 < x Initial program 33.5%
unpow233.5%
unpow233.5%
difference-of-squares33.6%
*-commutative33.6%
+-commutative33.6%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
associate-+r+99.9%
count-299.9%
fma-def99.9%
Simplified99.9%
Taylor expanded in eps around 0 79.7%
if -2.2999999999999999e-83 < x < 2.50000000000000017e-38Initial program 95.1%
unpow295.1%
unpow295.1%
difference-of-squares95.1%
*-commutative95.1%
+-commutative95.1%
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.7%
unpow294.7%
Simplified94.7%
Final simplification90.9%
(FPCore (x eps) :precision binary64 (if (<= x -2.2e-83) (* eps (+ x x)) (if (<= x 2.5e-38) (* eps eps) (* 2.0 (* x eps)))))
double code(double x, double eps) {
double tmp;
if (x <= -2.2e-83) {
tmp = eps * (x + x);
} else if (x <= 2.5e-38) {
tmp = eps * eps;
} else {
tmp = 2.0 * (x * eps);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-2.2d-83)) then
tmp = eps * (x + x)
else if (x <= 2.5d-38) then
tmp = eps * eps
else
tmp = 2.0d0 * (x * eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -2.2e-83) {
tmp = eps * (x + x);
} else if (x <= 2.5e-38) {
tmp = eps * eps;
} else {
tmp = 2.0 * (x * eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -2.2e-83: tmp = eps * (x + x) elif x <= 2.5e-38: tmp = eps * eps else: tmp = 2.0 * (x * eps) return tmp
function code(x, eps) tmp = 0.0 if (x <= -2.2e-83) tmp = Float64(eps * Float64(x + x)); elseif (x <= 2.5e-38) tmp = Float64(eps * eps); else tmp = Float64(2.0 * Float64(x * eps)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -2.2e-83) tmp = eps * (x + x); elseif (x <= 2.5e-38) tmp = eps * eps; else tmp = 2.0 * (x * eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -2.2e-83], N[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.5e-38], N[(eps * eps), $MachinePrecision], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.2 \cdot 10^{-83}:\\
\;\;\;\;\varepsilon \cdot \left(x + x\right)\\
\mathbf{elif}\;x \leq 2.5 \cdot 10^{-38}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\end{array}
\end{array}
if x < -2.20000000000000008e-83Initial program 38.6%
unpow238.6%
unpow238.6%
difference-of-squares38.7%
*-commutative38.7%
+-commutative38.7%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
associate-+r+99.9%
count-299.9%
fma-def99.9%
Simplified99.9%
fma-udef99.9%
distribute-rgt-in99.9%
fma-def100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 74.2%
*-commutative74.2%
count-274.2%
distribute-rgt-out74.2%
Simplified74.2%
if -2.20000000000000008e-83 < x < 2.50000000000000017e-38Initial program 95.1%
unpow295.1%
unpow295.1%
difference-of-squares95.1%
*-commutative95.1%
+-commutative95.1%
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.7%
unpow294.7%
Simplified94.7%
if 2.50000000000000017e-38 < x Initial program 16.8%
unpow216.8%
unpow216.8%
difference-of-squares16.8%
*-commutative16.8%
+-commutative16.8%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
associate-+r+99.9%
count-299.9%
fma-def99.9%
Simplified99.9%
Taylor expanded in eps around 0 97.6%
Final simplification90.9%
(FPCore (x eps) :precision binary64 (* eps (+ eps (+ x x))))
double code(double x, double eps) {
return eps * (eps + (x + x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * (eps + (x + x))
end function
public static double code(double x, double eps) {
return eps * (eps + (x + x));
}
def code(x, eps): return eps * (eps + (x + x))
function code(x, eps) return Float64(eps * Float64(eps + Float64(x + x))) end
function tmp = code(x, eps) tmp = eps * (eps + (x + x)); end
code[x_, eps_] := N[(eps * N[(eps + N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(\varepsilon + \left(x + x\right)\right)
\end{array}
Initial program 79.7%
unpow279.7%
Simplified79.7%
unpow279.7%
+-commutative79.7%
distribute-lft-in79.7%
+-commutative79.7%
add-sqr-sqrt38.7%
sqrt-prod72.4%
unpow272.4%
pow1/272.4%
add-sqr-sqrt38.7%
sqrt-prod79.5%
pow1/279.5%
pow-prod-down68.6%
*-commutative68.6%
pow-prod-down79.5%
pow1/279.5%
sqrt-prod38.7%
add-sqr-sqrt72.4%
pow1/272.4%
unpow272.4%
sqrt-prod38.7%
add-sqr-sqrt79.7%
+-commutative79.7%
Applied egg-rr79.7%
+-commutative79.7%
associate--l+79.7%
*-commutative79.7%
Applied egg-rr79.7%
Taylor expanded in x around 0 99.9%
unpow299.9%
+-commutative99.9%
*-commutative99.9%
associate-*r*100.0%
count-2100.0%
distribute-rgt-in100.0%
+-commutative100.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 79.7%
unpow279.7%
unpow279.7%
difference-of-squares79.7%
*-commutative79.7%
+-commutative79.7%
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 78.3%
unpow278.3%
Simplified78.3%
Final simplification78.3%
herbie shell --seed 2023238
(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)))