
(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 (+ eps (* 2.0 x))))
double code(double x, double eps) {
return eps * (eps + (2.0 * x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * (eps + (2.0d0 * x))
end function
public static double code(double x, double eps) {
return eps * (eps + (2.0 * x));
}
def code(x, eps): return eps * (eps + (2.0 * x))
function code(x, eps) return Float64(eps * Float64(eps + Float64(2.0 * x))) end
function tmp = code(x, eps) tmp = eps * (eps + (2.0 * x)); end
code[x_, eps_] := N[(eps * N[(eps + N[(2.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(\varepsilon + 2 \cdot x\right)
\end{array}
Initial program 72.7%
unpow272.7%
unpow272.7%
difference-of-squares72.7%
*-commutative72.7%
+-commutative72.7%
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 (if (or (<= x -3.2e-74) (not (<= x 1.8e-103))) (* x (+ eps eps)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -3.2e-74) || !(x <= 1.8e-103)) {
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 <= (-3.2d-74)) .or. (.not. (x <= 1.8d-103))) 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 <= -3.2e-74) || !(x <= 1.8e-103)) {
tmp = x * (eps + eps);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -3.2e-74) or not (x <= 1.8e-103): tmp = x * (eps + eps) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -3.2e-74) || !(x <= 1.8e-103)) 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 <= -3.2e-74) || ~((x <= 1.8e-103))) tmp = x * (eps + eps); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -3.2e-74], N[Not[LessEqual[x, 1.8e-103]], $MachinePrecision]], N[(x * N[(eps + eps), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-74} \lor \neg \left(x \leq 1.8 \cdot 10^{-103}\right):\\
\;\;\;\;x \cdot \left(\varepsilon + \varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -3.1999999999999999e-74 or 1.7999999999999999e-103 < x Initial program 28.2%
unpow228.2%
unpow228.2%
difference-of-squares28.2%
*-commutative28.2%
+-commutative28.2%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
Simplified99.9%
Taylor expanded in eps around 0 86.1%
*-commutative86.1%
count-286.1%
distribute-lft-out86.2%
Simplified86.2%
if -3.1999999999999999e-74 < x < 1.7999999999999999e-103Initial program 95.3%
unpow295.3%
unpow295.3%
difference-of-squares95.3%
*-commutative95.3%
+-commutative95.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 94.5%
Final simplification91.7%
(FPCore (x eps) :precision binary64 (* eps (+ x (+ eps x))))
double code(double x, double eps) {
return eps * (x + (eps + x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * (x + (eps + x))
end function
public static double code(double x, double eps) {
return eps * (x + (eps + x));
}
def code(x, eps): return eps * (x + (eps + x))
function code(x, eps) return Float64(eps * Float64(x + Float64(eps + x))) end
function tmp = code(x, eps) tmp = eps * (x + (eps + x)); end
code[x_, eps_] := N[(eps * N[(x + N[(eps + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(x + \left(\varepsilon + x\right)\right)
\end{array}
Initial program 72.7%
unpow272.7%
unpow272.7%
difference-of-squares72.7%
*-commutative72.7%
+-commutative72.7%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.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 72.7%
unpow272.7%
unpow272.7%
difference-of-squares72.7%
*-commutative72.7%
+-commutative72.7%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 71.1%
Final simplification71.1%
herbie shell --seed 2023279
(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)))