
(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 (if (<= x -1.85e-47) (- (pow (+ x eps) 2.0) (pow x 2.0)) (fma eps eps (* eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.85e-47) {
tmp = pow((x + eps), 2.0) - pow(x, 2.0);
} else {
tmp = fma(eps, eps, (eps * (x * 2.0)));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (x <= -1.85e-47) tmp = Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)); else tmp = fma(eps, eps, Float64(eps * Float64(x * 2.0))); end return tmp end
code[x_, eps_] := If[LessEqual[x, -1.85e-47], N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], N[(eps * eps + N[(eps * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.85 \cdot 10^{-47}:\\
\;\;\;\;{\left(x + \varepsilon\right)}^{2} - {x}^{2}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\varepsilon, \varepsilon, \varepsilon \cdot \left(x \cdot 2\right)\right)\\
\end{array}
\end{array}
if x < -1.85e-47Initial program 78.8%
if -1.85e-47 < x Initial program 82.4%
+-commutative82.4%
sqr-pow82.4%
sqr-pow82.4%
unpow282.4%
unpow282.4%
difference-of-squares82.4%
associate--l+98.9%
+-inverses98.9%
+-rgt-identity98.9%
*-commutative98.9%
associate-+l+98.9%
*-lft-identity98.9%
metadata-eval98.9%
distribute-rgt-out98.9%
distribute-lft-out98.9%
metadata-eval98.9%
metadata-eval98.9%
metadata-eval98.9%
Simplified98.9%
distribute-lft-in99.0%
fma-def99.0%
Applied egg-rr99.0%
Final simplification96.9%
(FPCore (x eps) :precision binary64 (if (<= x -2.05e-46) (* (+ x (+ x eps)) (- (+ x eps) x)) (fma eps eps (* eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -2.05e-46) {
tmp = (x + (x + eps)) * ((x + eps) - x);
} else {
tmp = fma(eps, eps, (eps * (x * 2.0)));
}
return tmp;
}
function code(x, eps) tmp = 0.0 if (x <= -2.05e-46) tmp = Float64(Float64(x + Float64(x + eps)) * Float64(Float64(x + eps) - x)); else tmp = fma(eps, eps, Float64(eps * Float64(x * 2.0))); end return tmp end
code[x_, eps_] := If[LessEqual[x, -2.05e-46], N[(N[(x + N[(x + eps), $MachinePrecision]), $MachinePrecision] * N[(N[(x + eps), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision], N[(eps * eps + N[(eps * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.05 \cdot 10^{-46}:\\
\;\;\;\;\left(x + \left(x + \varepsilon\right)\right) \cdot \left(\left(x + \varepsilon\right) - x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\varepsilon, \varepsilon, \varepsilon \cdot \left(x \cdot 2\right)\right)\\
\end{array}
\end{array}
if x < -2.05e-46Initial program 78.8%
unpow278.8%
unpow278.8%
difference-of-squares78.6%
+-commutative78.6%
+-commutative78.6%
Applied egg-rr78.6%
if -2.05e-46 < x Initial program 82.4%
+-commutative82.4%
sqr-pow82.4%
sqr-pow82.4%
unpow282.4%
unpow282.4%
difference-of-squares82.4%
associate--l+98.9%
+-inverses98.9%
+-rgt-identity98.9%
*-commutative98.9%
associate-+l+98.9%
*-lft-identity98.9%
metadata-eval98.9%
distribute-rgt-out98.9%
distribute-lft-out98.9%
metadata-eval98.9%
metadata-eval98.9%
metadata-eval98.9%
Simplified98.9%
distribute-lft-in99.0%
fma-def99.0%
Applied egg-rr99.0%
Final simplification96.9%
(FPCore (x eps) :precision binary64 (if (<= x -1.9e-47) (* (+ x (+ x eps)) (- (+ x eps) x)) (* eps (+ eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.9e-47) {
tmp = (x + (x + eps)) * ((x + eps) - x);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-1.9d-47)) then
tmp = (x + (x + eps)) * ((x + eps) - x)
else
tmp = eps * (eps + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -1.9e-47) {
tmp = (x + (x + eps)) * ((x + eps) - x);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.9e-47: tmp = (x + (x + eps)) * ((x + eps) - x) else: tmp = eps * (eps + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.9e-47) tmp = Float64(Float64(x + Float64(x + eps)) * Float64(Float64(x + eps) - x)); else tmp = Float64(eps * Float64(eps + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -1.9e-47) tmp = (x + (x + eps)) * ((x + eps) - x); else tmp = eps * (eps + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.9e-47], N[(N[(x + N[(x + eps), $MachinePrecision]), $MachinePrecision] * N[(N[(x + eps), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision], N[(eps * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.9 \cdot 10^{-47}:\\
\;\;\;\;\left(x + \left(x + \varepsilon\right)\right) \cdot \left(\left(x + \varepsilon\right) - x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(\varepsilon + x \cdot 2\right)\\
\end{array}
\end{array}
if x < -1.90000000000000007e-47Initial program 78.8%
unpow278.8%
unpow278.8%
difference-of-squares78.6%
+-commutative78.6%
+-commutative78.6%
Applied egg-rr78.6%
if -1.90000000000000007e-47 < x Initial program 82.4%
+-commutative82.4%
sqr-pow82.4%
sqr-pow82.4%
unpow282.4%
unpow282.4%
difference-of-squares82.4%
associate--l+98.9%
+-inverses98.9%
+-rgt-identity98.9%
*-commutative98.9%
associate-+l+98.9%
*-lft-identity98.9%
metadata-eval98.9%
distribute-rgt-out98.9%
distribute-lft-out98.9%
metadata-eval98.9%
metadata-eval98.9%
metadata-eval98.9%
Simplified98.9%
Final simplification96.9%
(FPCore (x eps) :precision binary64 (if (<= x -1.1e-35) (* (* x 2.0) (- (+ x eps) x)) (* eps (+ eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.1e-35) {
tmp = (x * 2.0) * ((x + eps) - x);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-1.1d-35)) then
tmp = (x * 2.0d0) * ((x + eps) - x)
else
tmp = eps * (eps + (x * 2.0d0))
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -1.1e-35) {
tmp = (x * 2.0) * ((x + eps) - x);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.1e-35: tmp = (x * 2.0) * ((x + eps) - x) else: tmp = eps * (eps + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.1e-35) tmp = Float64(Float64(x * 2.0) * Float64(Float64(x + eps) - x)); else tmp = Float64(eps * Float64(eps + Float64(x * 2.0))); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -1.1e-35) tmp = (x * 2.0) * ((x + eps) - x); else tmp = eps * (eps + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.1e-35], N[(N[(x * 2.0), $MachinePrecision] * N[(N[(x + eps), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision], N[(eps * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.1 \cdot 10^{-35}:\\
\;\;\;\;\left(x \cdot 2\right) \cdot \left(\left(x + \varepsilon\right) - x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(\varepsilon + x \cdot 2\right)\\
\end{array}
\end{array}
if x < -1.09999999999999997e-35Initial program 78.6%
unpow278.6%
unpow278.6%
difference-of-squares78.6%
+-commutative78.6%
+-commutative78.6%
Applied egg-rr78.6%
Taylor expanded in eps around 0 73.1%
if -1.09999999999999997e-35 < x Initial program 82.3%
+-commutative82.3%
sqr-pow82.3%
sqr-pow82.3%
unpow282.3%
unpow282.3%
difference-of-squares82.3%
associate--l+97.8%
+-inverses97.8%
+-rgt-identity97.8%
*-commutative97.8%
associate-+l+97.8%
*-lft-identity97.8%
metadata-eval97.8%
distribute-rgt-out97.8%
distribute-lft-out97.8%
metadata-eval97.8%
metadata-eval97.8%
metadata-eval97.8%
Simplified97.8%
Final simplification96.1%
(FPCore (x eps) :precision binary64 (if (<= x -6.5e-36) (* (* x 2.0) (- (+ x eps) x)) (+ (* eps (+ x eps)) (* x eps))))
double code(double x, double eps) {
double tmp;
if (x <= -6.5e-36) {
tmp = (x * 2.0) * ((x + eps) - x);
} else {
tmp = (eps * (x + eps)) + (x * eps);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-6.5d-36)) then
tmp = (x * 2.0d0) * ((x + eps) - x)
else
tmp = (eps * (x + eps)) + (x * eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -6.5e-36) {
tmp = (x * 2.0) * ((x + eps) - x);
} else {
tmp = (eps * (x + eps)) + (x * eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -6.5e-36: tmp = (x * 2.0) * ((x + eps) - x) else: tmp = (eps * (x + eps)) + (x * eps) return tmp
function code(x, eps) tmp = 0.0 if (x <= -6.5e-36) tmp = Float64(Float64(x * 2.0) * Float64(Float64(x + eps) - x)); else tmp = Float64(Float64(eps * Float64(x + eps)) + Float64(x * eps)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -6.5e-36) tmp = (x * 2.0) * ((x + eps) - x); else tmp = (eps * (x + eps)) + (x * eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -6.5e-36], N[(N[(x * 2.0), $MachinePrecision] * N[(N[(x + eps), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision], N[(N[(eps * N[(x + eps), $MachinePrecision]), $MachinePrecision] + N[(x * eps), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.5 \cdot 10^{-36}:\\
\;\;\;\;\left(x \cdot 2\right) \cdot \left(\left(x + \varepsilon\right) - x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(x + \varepsilon\right) + x \cdot \varepsilon\\
\end{array}
\end{array}
if x < -6.50000000000000012e-36Initial program 78.6%
unpow278.6%
unpow278.6%
difference-of-squares78.6%
+-commutative78.6%
+-commutative78.6%
Applied egg-rr78.6%
Taylor expanded in eps around 0 73.1%
if -6.50000000000000012e-36 < x Initial program 82.3%
unpow282.3%
unpow282.3%
difference-of-squares82.3%
+-commutative82.3%
+-commutative82.3%
Applied egg-rr82.3%
Taylor expanded in eps around 0 97.8%
*-commutative97.8%
distribute-lft-in97.8%
Applied egg-rr97.8%
Final simplification96.1%
(FPCore (x eps) :precision binary64 (* eps (+ eps (* x 2.0))))
double code(double x, double eps) {
return eps * (eps + (x * 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * (eps + (x * 2.0d0))
end function
public static double code(double x, double eps) {
return eps * (eps + (x * 2.0));
}
def code(x, eps): return eps * (eps + (x * 2.0))
function code(x, eps) return Float64(eps * Float64(eps + Float64(x * 2.0))) end
function tmp = code(x, eps) tmp = eps * (eps + (x * 2.0)); end
code[x_, eps_] := N[(eps * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(\varepsilon + x \cdot 2\right)
\end{array}
Initial program 82.0%
+-commutative82.0%
sqr-pow82.0%
sqr-pow82.0%
unpow282.0%
unpow282.0%
difference-of-squares82.0%
associate--l+93.2%
+-inverses93.2%
+-rgt-identity93.2%
*-commutative93.2%
associate-+l+93.2%
*-lft-identity93.2%
metadata-eval93.2%
distribute-rgt-out93.2%
distribute-lft-out93.2%
metadata-eval93.2%
metadata-eval93.2%
metadata-eval93.2%
Simplified93.2%
Final simplification93.2%
(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 82.0%
+-commutative82.0%
sqr-pow82.0%
sqr-pow82.0%
unpow282.0%
unpow282.0%
difference-of-squares82.0%
associate--l+93.2%
+-inverses93.2%
+-rgt-identity93.2%
*-commutative93.2%
associate-+l+93.2%
*-lft-identity93.2%
metadata-eval93.2%
distribute-rgt-out93.2%
distribute-lft-out93.2%
metadata-eval93.2%
metadata-eval93.2%
metadata-eval93.2%
Simplified93.2%
Taylor expanded in eps around 0 56.1%
Final simplification56.1%
herbie shell --seed 2024031
(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)))