
(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 (fma (+ x x) eps (* eps eps)))
double code(double x, double eps) {
return fma((x + x), eps, (eps * eps));
}
function code(x, eps) return fma(Float64(x + x), eps, Float64(eps * eps)) end
code[x_, eps_] := N[(N[(x + x), $MachinePrecision] * eps + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x + x, \varepsilon, \varepsilon \cdot \varepsilon\right)
\end{array}
Initial program 74.2%
unpow274.2%
unpow274.2%
difference-of-squares74.2%
*-commutative74.2%
+-commutative74.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
associate-+r+100.0%
distribute-rgt-in100.0%
fma-def100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -8.2e-104) (not (<= x 2.2e-99))) (* x (+ eps eps)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -8.2e-104) || !(x <= 2.2e-99)) {
tmp = x * (eps + 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.2d-104)) .or. (.not. (x <= 2.2d-99))) then
tmp = x * (eps + eps)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -8.2e-104) || !(x <= 2.2e-99)) {
tmp = x * (eps + eps);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -8.2e-104) or not (x <= 2.2e-99): tmp = x * (eps + eps) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -8.2e-104) || !(x <= 2.2e-99)) tmp = Float64(x * Float64(eps + eps)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -8.2e-104) || ~((x <= 2.2e-99))) tmp = x * (eps + eps); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -8.2e-104], N[Not[LessEqual[x, 2.2e-99]], $MachinePrecision]], N[(x * N[(eps + eps), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.2 \cdot 10^{-104} \lor \neg \left(x \leq 2.2 \cdot 10^{-99}\right):\\
\;\;\;\;x \cdot \left(\varepsilon + \varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -8.19999999999999968e-104 or 2.20000000000000004e-99 < x Initial program 34.2%
unpow234.2%
unpow234.2%
difference-of-squares34.1%
*-commutative34.1%
+-commutative34.1%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in eps around 0 87.6%
*-commutative87.6%
count-287.6%
distribute-lft-out87.6%
Simplified87.6%
if -8.19999999999999968e-104 < x < 2.20000000000000004e-99Initial program 96.6%
unpow296.6%
unpow296.6%
difference-of-squares96.6%
*-commutative96.6%
+-commutative96.6%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 95.4%
Final simplification92.6%
(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 74.2%
unpow274.2%
unpow274.2%
difference-of-squares74.2%
*-commutative74.2%
+-commutative74.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 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.2%
unpow274.2%
unpow274.2%
difference-of-squares74.2%
*-commutative74.2%
+-commutative74.2%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 72.4%
Final simplification72.4%
herbie shell --seed 2023271
(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)))