
(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 eps eps (* (* x 2.0) eps)))
double code(double x, double eps) {
return fma(eps, eps, ((x * 2.0) * eps));
}
function code(x, eps) return fma(eps, eps, Float64(Float64(x * 2.0) * eps)) end
code[x_, eps_] := N[(eps * eps + N[(N[(x * 2.0), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, \varepsilon, \left(x \cdot 2\right) \cdot \varepsilon\right)
\end{array}
Initial program 75.5%
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
*-commutativeN/A
associate-*r/N/A
associate-*r/N/A
distribute-lft-inN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.0%
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (<= (- (pow (+ x eps) 2.0) (pow x 2.0)) 0.0) (* (* x 2.0) eps) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((pow((x + eps), 2.0) - pow(x, 2.0)) <= 0.0) {
tmp = (x * 2.0) * 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 + eps) ** 2.0d0) - (x ** 2.0d0)) <= 0.0d0) then
tmp = (x * 2.0d0) * eps
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((Math.pow((x + eps), 2.0) - Math.pow(x, 2.0)) <= 0.0) {
tmp = (x * 2.0) * eps;
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (math.pow((x + eps), 2.0) - math.pow(x, 2.0)) <= 0.0: tmp = (x * 2.0) * eps else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if (Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = Float64(Float64(x * 2.0) * eps); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((((x + eps) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = (x * 2.0) * eps; else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(x * 2.0), $MachinePrecision] * eps), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\left(x + \varepsilon\right)}^{2} - {x}^{2} \leq 0:\\
\;\;\;\;\left(x \cdot 2\right) \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\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 59.8%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6498.9
Applied rewrites98.9%
Applied rewrites99.0%
if 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) Initial program 98.8%
Taylor expanded in eps around inf
unpow2N/A
lower-*.f6493.8
Applied rewrites93.8%
Final simplification96.9%
(FPCore (x eps) :precision binary64 (* (fma x 2.0 eps) eps))
double code(double x, double eps) {
return fma(x, 2.0, eps) * eps;
}
function code(x, eps) return Float64(fma(x, 2.0, eps) * eps) end
code[x_, eps_] := N[(N[(x * 2.0 + eps), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 2, \varepsilon\right) \cdot \varepsilon
\end{array}
Initial program 75.5%
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
*-commutativeN/A
associate-*r/N/A
associate-*r/N/A
distribute-lft-inN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites100.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 75.5%
Taylor expanded in eps around inf
unpow2N/A
lower-*.f6472.6
Applied rewrites72.6%
(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.5%
lift--.f64N/A
sub-negN/A
lift-pow.f64N/A
unpow2N/A
lift-+.f64N/A
distribute-lft-inN/A
associate-+l+N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
lower-fma.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
distribute-rgt-inN/A
+-commutativeN/A
lift-+.f64N/A
*-commutativeN/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
Applied rewrites62.6%
Taylor expanded in eps around 0
distribute-lft1-inN/A
metadata-evalN/A
mul0-lft37.9
Applied rewrites37.9%
herbie shell --seed 2024277
(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)))