
(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 6 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 eps (* (+ eps x) eps)))
double code(double x, double eps) {
return fma(x, eps, ((eps + x) * eps));
}
function code(x, eps) return fma(x, eps, Float64(Float64(eps + x) * eps)) end
code[x_, eps_] := N[(x * eps + N[(N[(eps + x), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, \varepsilon, \left(\varepsilon + x\right) \cdot \varepsilon\right)
\end{array}
Initial program 75.3%
Taylor expanded in eps around 0
distribute-rgt-inN/A
unpow2N/A
*-rgt-identityN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
unpow2N/A
associate-/l*N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
distribute-lft-inN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Applied rewrites100.0%
Applied rewrites100.0%
(FPCore (x eps) :precision binary64 (if (<= (- (pow (+ eps x) 2.0) (pow x 2.0)) 0.0) (* (* 2.0 eps) x) (fma (+ eps x) eps 0.0)))
double code(double x, double eps) {
double tmp;
if ((pow((eps + x), 2.0) - pow(x, 2.0)) <= 0.0) {
tmp = (2.0 * eps) * x;
} else {
tmp = fma((eps + x), eps, 0.0);
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (Float64((Float64(eps + x) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = Float64(Float64(2.0 * eps) * x); else tmp = fma(Float64(eps + x), eps, 0.0); end return tmp end
code[x_, eps_] := If[LessEqual[N[(N[Power[N[(eps + x), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(2.0 * eps), $MachinePrecision] * x), $MachinePrecision], N[(N[(eps + x), $MachinePrecision] * eps + 0.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\left(\varepsilon + x\right)}^{2} - {x}^{2} \leq 0:\\
\;\;\;\;\left(2 \cdot \varepsilon\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right)\\
\end{array}
\end{array}
if (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) < 0.0Initial program 60.2%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
lower-*.f6499.6
Applied rewrites99.6%
Applied rewrites99.6%
if 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) Initial program 99.7%
lift--.f64N/A
sub-negN/A
lift-pow.f64N/A
unpow2N/A
lift-+.f64N/A
+-commutativeN/A
distribute-lft-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lift-pow.f64N/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6499.7
Applied rewrites99.7%
Taylor expanded in eps around 0
distribute-lft1-inN/A
metadata-evalN/A
mul0-lft94.9
Applied rewrites94.9%
Final simplification97.8%
(FPCore (x eps) :precision binary64 (* (+ (+ eps x) x) eps))
double code(double x, double eps) {
return ((eps + x) + x) * eps;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((eps + x) + x) * eps
end function
public static double code(double x, double eps) {
return ((eps + x) + x) * eps;
}
def code(x, eps): return ((eps + x) + x) * eps
function code(x, eps) return Float64(Float64(Float64(eps + x) + x) * eps) end
function tmp = code(x, eps) tmp = ((eps + x) + x) * eps; end
code[x_, eps_] := N[(N[(N[(eps + x), $MachinePrecision] + x), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(\varepsilon + x\right) + x\right) \cdot \varepsilon
\end{array}
Initial program 75.3%
Taylor expanded in eps around 0
distribute-rgt-inN/A
unpow2N/A
*-rgt-identityN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
unpow2N/A
associate-/l*N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
distribute-lft-inN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Applied rewrites100.0%
(FPCore (x eps) :precision binary64 (fma (+ eps x) eps 0.0))
double code(double x, double eps) {
return fma((eps + x), eps, 0.0);
}
function code(x, eps) return fma(Float64(eps + x), eps, 0.0) end
code[x_, eps_] := N[(N[(eps + x), $MachinePrecision] * eps + 0.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right)
\end{array}
Initial program 75.3%
lift--.f64N/A
sub-negN/A
lift-pow.f64N/A
unpow2N/A
lift-+.f64N/A
+-commutativeN/A
distribute-lft-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lift-pow.f64N/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6466.5
Applied rewrites66.5%
Taylor expanded in eps around 0
distribute-lft1-inN/A
metadata-evalN/A
mul0-lft76.3
Applied rewrites76.3%
(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 75.3%
Taylor expanded in eps around inf
unpow2N/A
lower-*.f6472.9
Applied rewrites72.9%
(FPCore (x eps) :precision binary64 0.0)
double code(double x, double eps) {
return 0.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.0d0
end function
public static double code(double x, double eps) {
return 0.0;
}
def code(x, eps): return 0.0
function code(x, eps) return 0.0 end
function tmp = code(x, eps) tmp = 0.0; end
code[x_, eps_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 75.3%
lift--.f64N/A
sub-negN/A
lift-pow.f64N/A
unpow2N/A
lift-+.f64N/A
+-commutativeN/A
distribute-lft-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lift-pow.f64N/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6466.5
Applied rewrites66.5%
Taylor expanded in eps around 0
distribute-lft1-inN/A
metadata-evalN/A
mul0-lft38.8
Applied rewrites38.8%
herbie shell --seed 2024276
(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)))