
(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 7 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 (* eps (+ x x))))
double code(double x, double eps) {
return fma(eps, eps, (eps * (x + x)));
}
function code(x, eps) return fma(eps, eps, Float64(eps * Float64(x + x))) end
code[x_, eps_] := N[(eps * eps + N[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, \varepsilon, \varepsilon \cdot \left(x + x\right)\right)
\end{array}
Initial program 76.9%
Taylor expanded in x around 0
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-lowering-*.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
Simplified100.0%
+-commutativeN/A
distribute-rgt-inN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
*-commutativeN/A
count-2N/A
+-lowering-+.f64100.0
Applied egg-rr100.0%
(FPCore (x eps) :precision binary64 (if (<= (- (pow (+ eps x) 2.0) (pow x 2.0)) 0.0) (* eps (+ x x)) (* eps (+ eps x))))
double code(double x, double eps) {
double tmp;
if ((pow((eps + x), 2.0) - pow(x, 2.0)) <= 0.0) {
tmp = eps * (x + x);
} else {
tmp = eps * (eps + x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if ((((eps + x) ** 2.0d0) - (x ** 2.0d0)) <= 0.0d0) then
tmp = eps * (x + x)
else
tmp = eps * (eps + x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((Math.pow((eps + x), 2.0) - Math.pow(x, 2.0)) <= 0.0) {
tmp = eps * (x + x);
} else {
tmp = eps * (eps + x);
}
return tmp;
}
def code(x, eps): tmp = 0 if (math.pow((eps + x), 2.0) - math.pow(x, 2.0)) <= 0.0: tmp = eps * (x + x) else: tmp = eps * (eps + x) return tmp
function code(x, eps) tmp = 0.0 if (Float64((Float64(eps + x) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = Float64(eps * Float64(x + x)); else tmp = Float64(eps * Float64(eps + x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((((eps + x) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = eps * (x + x); else tmp = eps * (eps + x); end tmp_2 = 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[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision], N[(eps * N[(eps + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\left(\varepsilon + x\right)}^{2} - {x}^{2} \leq 0:\\
\;\;\;\;\varepsilon \cdot \left(x + x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(\varepsilon + x\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 63.7%
Taylor expanded in x around inf
associate-*r*N/A
*-commutativeN/A
*-lowering-*.f64N/A
*-lowering-*.f6498.9
Simplified98.9%
associate-*r*N/A
*-lowering-*.f64N/A
*-commutativeN/A
count-2N/A
+-lowering-+.f6498.9
Applied egg-rr98.9%
if 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) Initial program 98.5%
Taylor expanded in x around 0
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-lowering-*.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
Simplified99.9%
*-commutativeN/A
count-2N/A
associate-+r+N/A
+-commutativeN/A
+-lowering-+.f64N/A
+-commutativeN/A
+-lowering-+.f6499.9
Applied egg-rr99.9%
Taylor expanded in eps around inf
Simplified93.4%
Final simplification96.8%
(FPCore (x eps) :precision binary64 (if (<= (- (pow (+ eps x) 2.0) (pow x 2.0)) 0.0) (* eps x) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((pow((eps + x), 2.0) - pow(x, 2.0)) <= 0.0) {
tmp = eps * x;
} 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 ((((eps + x) ** 2.0d0) - (x ** 2.0d0)) <= 0.0d0) then
tmp = eps * x
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((Math.pow((eps + x), 2.0) - Math.pow(x, 2.0)) <= 0.0) {
tmp = eps * x;
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (math.pow((eps + x), 2.0) - math.pow(x, 2.0)) <= 0.0: tmp = eps * x else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if (Float64((Float64(eps + x) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = Float64(eps * x); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((((eps + x) ^ 2.0) - (x ^ 2.0)) <= 0.0) tmp = eps * x; else tmp = eps * eps; end tmp_2 = 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[(eps * x), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\left(\varepsilon + x\right)}^{2} - {x}^{2} \leq 0:\\
\;\;\;\;\varepsilon \cdot x\\
\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 63.7%
Taylor expanded in x around 0
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-lowering-*.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
Simplified100.0%
*-commutativeN/A
count-2N/A
associate-+r+N/A
+-commutativeN/A
+-lowering-+.f64N/A
+-commutativeN/A
+-lowering-+.f64100.0
Applied egg-rr100.0%
Taylor expanded in eps around inf
Simplified67.1%
Taylor expanded in eps around 0
*-lowering-*.f6467.1
Simplified67.1%
if 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) Initial program 98.5%
Taylor expanded in x around 0
unpow2N/A
*-lowering-*.f6493.0
Simplified93.0%
Final simplification76.9%
(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 76.9%
Taylor expanded in x around 0
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-lowering-*.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
Simplified100.0%
*-commutativeN/A
count-2N/A
associate-+r+N/A
+-commutativeN/A
+-lowering-+.f64N/A
+-commutativeN/A
+-lowering-+.f64100.0
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (* eps (+ eps x)))
double code(double x, double eps) {
return eps * (eps + x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * (eps + x)
end function
public static double code(double x, double eps) {
return eps * (eps + x);
}
def code(x, eps): return eps * (eps + x)
function code(x, eps) return Float64(eps * Float64(eps + x)) end
function tmp = code(x, eps) tmp = eps * (eps + x); end
code[x_, eps_] := N[(eps * N[(eps + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(\varepsilon + x\right)
\end{array}
Initial program 76.9%
Taylor expanded in x around 0
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
*-inversesN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
unpow2N/A
associate-*l/N/A
associate-*r/N/A
*-commutativeN/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-lowering-*.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
Simplified100.0%
*-commutativeN/A
count-2N/A
associate-+r+N/A
+-commutativeN/A
+-lowering-+.f64N/A
+-commutativeN/A
+-lowering-+.f64100.0
Applied egg-rr100.0%
Taylor expanded in eps around inf
Simplified77.1%
(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 76.9%
Taylor expanded in x around 0
unpow2N/A
*-lowering-*.f6473.7
Simplified73.7%
(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 76.9%
sub-negN/A
unpow2N/A
distribute-lft-inN/A
associate-+l+N/A
*-commutativeN/A
distribute-rgt-inN/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
+-lowering-+.f64N/A
distribute-rgt-inN/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
+-lowering-+.f64N/A
unpow2N/A
distribute-rgt-neg-inN/A
*-lowering-*.f64N/A
neg-lowering-neg.f6466.4
Applied egg-rr66.4%
Taylor expanded in eps around 0
distribute-lft1-inN/A
metadata-evalN/A
mul0-lft40.7
Simplified40.7%
herbie shell --seed 2024199
(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)))