
(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 74.7%
unpow274.7%
unpow274.7%
difference-of-squares74.7%
*-commutative74.7%
+-commutative74.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-in99.9%
fma-def100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -6e-81) (not (<= x 4.1e-98))) (* 2.0 (* x eps)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -6e-81) || !(x <= 4.1e-98)) {
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 <= (-6d-81)) .or. (.not. (x <= 4.1d-98))) 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 <= -6e-81) || !(x <= 4.1e-98)) {
tmp = 2.0 * (x * eps);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -6e-81) or not (x <= 4.1e-98): tmp = 2.0 * (x * eps) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -6e-81) || !(x <= 4.1e-98)) 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 <= -6e-81) || ~((x <= 4.1e-98))) tmp = 2.0 * (x * eps); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -6e-81], N[Not[LessEqual[x, 4.1e-98]], $MachinePrecision]], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{-81} \lor \neg \left(x \leq 4.1 \cdot 10^{-98}\right):\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -5.9999999999999998e-81 or 4.0999999999999998e-98 < x Initial program 28.4%
unpow228.4%
unpow228.4%
difference-of-squares28.4%
*-commutative28.4%
+-commutative28.4%
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 88.8%
if -5.9999999999999998e-81 < x < 4.0999999999999998e-98Initial program 94.9%
unpow294.9%
unpow294.9%
difference-of-squares95.0%
*-commutative95.0%
+-commutative95.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 93.7%
unpow293.7%
Simplified93.7%
Final simplification92.2%
(FPCore (x eps) :precision binary64 (if (<= x -1.16e-79) (* x (+ eps eps)) (if (<= x 1.4e-97) (* eps eps) (* 2.0 (* x eps)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.16e-79) {
tmp = x * (eps + eps);
} else if (x <= 1.4e-97) {
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 <= (-1.16d-79)) then
tmp = x * (eps + eps)
else if (x <= 1.4d-97) 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 <= -1.16e-79) {
tmp = x * (eps + eps);
} else if (x <= 1.4e-97) {
tmp = eps * eps;
} else {
tmp = 2.0 * (x * eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.16e-79: tmp = x * (eps + eps) elif x <= 1.4e-97: tmp = eps * eps else: tmp = 2.0 * (x * eps) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.16e-79) tmp = Float64(x * Float64(eps + eps)); elseif (x <= 1.4e-97) 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 <= -1.16e-79) tmp = x * (eps + eps); elseif (x <= 1.4e-97) tmp = eps * eps; else tmp = 2.0 * (x * eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.16e-79], N[(x * N[(eps + eps), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.4e-97], N[(eps * eps), $MachinePrecision], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.16 \cdot 10^{-79}:\\
\;\;\;\;x \cdot \left(\varepsilon + \varepsilon\right)\\
\mathbf{elif}\;x \leq 1.4 \cdot 10^{-97}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\end{array}
\end{array}
if x < -1.15999999999999999e-79Initial program 32.8%
unpow232.8%
unpow232.8%
difference-of-squares32.6%
*-commutative32.6%
+-commutative32.6%
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%
Taylor expanded in x around inf 91.1%
*-commutative91.1%
count-291.1%
distribute-lft-out91.1%
Simplified91.1%
if -1.15999999999999999e-79 < x < 1.4000000000000001e-97Initial program 94.9%
unpow294.9%
unpow294.9%
difference-of-squares95.0%
*-commutative95.0%
+-commutative95.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 93.7%
unpow293.7%
Simplified93.7%
if 1.4000000000000001e-97 < x Initial program 25.3%
unpow225.3%
unpow225.3%
difference-of-squares25.5%
*-commutative25.5%
+-commutative25.5%
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 87.2%
Final simplification92.2%
(FPCore (x eps) :precision binary64 (* eps (+ (* 2.0 x) eps)))
double code(double x, double eps) {
return eps * ((2.0 * x) + eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * ((2.0d0 * x) + eps)
end function
public static double code(double x, double eps) {
return eps * ((2.0 * x) + eps);
}
def code(x, eps): return eps * ((2.0 * x) + eps)
function code(x, eps) return Float64(eps * Float64(Float64(2.0 * x) + eps)) end
function tmp = code(x, eps) tmp = eps * ((2.0 * x) + eps); end
code[x_, eps_] := N[(eps * N[(N[(2.0 * x), $MachinePrecision] + eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(2 \cdot x + \varepsilon\right)
\end{array}
Initial program 74.7%
unpow274.7%
Simplified74.7%
unpow274.7%
difference-of-squares74.7%
+-commutative74.7%
+-commutative74.7%
Applied egg-rr74.7%
Taylor expanded in eps around 0 100.0%
Taylor expanded in eps around inf 100.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 74.7%
unpow274.7%
unpow274.7%
difference-of-squares74.7%
*-commutative74.7%
+-commutative74.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 71.8%
unpow271.8%
Simplified71.8%
Final simplification71.8%
herbie shell --seed 2023178
(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)))