
(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 (* x 2.0) eps (* eps eps)))
double code(double x, double eps) {
return fma((x * 2.0), eps, (eps * eps));
}
function code(x, eps) return fma(Float64(x * 2.0), eps, Float64(eps * eps)) end
code[x_, eps_] := N[(N[(x * 2.0), $MachinePrecision] * eps + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x \cdot 2, \varepsilon, \varepsilon \cdot \varepsilon\right)
\end{array}
Initial program 74.0%
+-commutative74.0%
unpow274.0%
unpow274.0%
difference-of-squares74.0%
sub-neg74.0%
distribute-lft-in74.0%
+-commutative74.0%
distribute-lft-in74.0%
+-commutative74.0%
sub-neg74.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
+-commutative100.0%
distribute-rgt-in100.0%
fma-define100.0%
pow2100.0%
Applied egg-rr100.0%
unpow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(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 74.0%
+-commutative74.0%
unpow274.0%
unpow274.0%
difference-of-squares74.0%
sub-neg74.0%
distribute-lft-in74.0%
+-commutative74.0%
distribute-lft-in74.0%
+-commutative74.0%
sub-neg74.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
distribute-lft-in100.0%
fma-define100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (* eps (+ (* x 2.0) eps)))
double code(double x, double eps) {
return eps * ((x * 2.0) + eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * ((x * 2.0d0) + eps)
end function
public static double code(double x, double eps) {
return eps * ((x * 2.0) + eps);
}
def code(x, eps): return eps * ((x * 2.0) + eps)
function code(x, eps) return Float64(eps * Float64(Float64(x * 2.0) + eps)) end
function tmp = code(x, eps) tmp = eps * ((x * 2.0) + eps); end
code[x_, eps_] := N[(eps * N[(N[(x * 2.0), $MachinePrecision] + eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(x \cdot 2 + \varepsilon\right)
\end{array}
Initial program 74.0%
+-commutative74.0%
unpow274.0%
unpow274.0%
difference-of-squares74.0%
sub-neg74.0%
distribute-lft-in74.0%
+-commutative74.0%
distribute-lft-in74.0%
+-commutative74.0%
sub-neg74.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (* 2.0 (* x eps)))
double code(double x, double eps) {
return 2.0 * (x * eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (x * eps)
end function
public static double code(double x, double eps) {
return 2.0 * (x * eps);
}
def code(x, eps): return 2.0 * (x * eps)
function code(x, eps) return Float64(2.0 * Float64(x * eps)) end
function tmp = code(x, eps) tmp = 2.0 * (x * eps); end
code[x_, eps_] := N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(x \cdot \varepsilon\right)
\end{array}
Initial program 74.0%
+-commutative74.0%
unpow274.0%
unpow274.0%
difference-of-squares74.0%
sub-neg74.0%
distribute-lft-in74.0%
+-commutative74.0%
distribute-lft-in74.0%
+-commutative74.0%
sub-neg74.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in eps around 0 63.7%
Final simplification63.7%
(FPCore (x eps) :precision binary64 (* (* x 2.0) eps))
double code(double x, double eps) {
return (x * 2.0) * eps;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (x * 2.0d0) * eps
end function
public static double code(double x, double eps) {
return (x * 2.0) * eps;
}
def code(x, eps): return (x * 2.0) * eps
function code(x, eps) return Float64(Float64(x * 2.0) * eps) end
function tmp = code(x, eps) tmp = (x * 2.0) * eps; end
code[x_, eps_] := N[(N[(x * 2.0), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot 2\right) \cdot \varepsilon
\end{array}
Initial program 74.0%
+-commutative74.0%
unpow274.0%
unpow274.0%
difference-of-squares74.0%
sub-neg74.0%
distribute-lft-in74.0%
+-commutative74.0%
distribute-lft-in74.0%
+-commutative74.0%
sub-neg74.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in eps around 0 63.7%
*-commutative63.7%
associate-*r*63.7%
*-commutative63.7%
Simplified63.7%
Final simplification63.7%
herbie shell --seed 2024055
(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)))