
(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 (if (<= x -6.2e-108) (* (+ x (+ x eps)) (- (+ x eps) x)) (* eps (+ eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -6.2e-108) {
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 <= (-6.2d-108)) 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 <= -6.2e-108) {
tmp = (x + (x + eps)) * ((x + eps) - x);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -6.2e-108: 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 <= -6.2e-108) 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 <= -6.2e-108) 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, -6.2e-108], 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 -6.2 \cdot 10^{-108}:\\
\;\;\;\;\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 < -6.20000000000000028e-108Initial program 86.2%
unpow286.2%
unpow286.2%
difference-of-squares86.2%
+-commutative86.2%
+-commutative86.2%
Applied egg-rr86.2%
if -6.20000000000000028e-108 < x Initial program 83.0%
+-commutative83.0%
unpow283.0%
unpow283.0%
difference-of-squares83.1%
*-commutative83.1%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
associate-+l+99.9%
*-lft-identity99.9%
metadata-eval99.9%
distribute-rgt-out99.9%
distribute-lft-out99.9%
metadata-eval99.9%
metadata-eval99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification97.7%
(FPCore (x eps) :precision binary64 (if (<= x -3.2e-54) (* (- (+ x eps) x) (* x 2.0)) (* eps (+ eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -3.2e-54) {
tmp = ((x + eps) - x) * (x * 2.0);
} 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 <= (-3.2d-54)) then
tmp = ((x + eps) - x) * (x * 2.0d0)
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 <= -3.2e-54) {
tmp = ((x + eps) - x) * (x * 2.0);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -3.2e-54: tmp = ((x + eps) - x) * (x * 2.0) else: tmp = eps * (eps + (x * 2.0)) return tmp
function code(x, eps) tmp = 0.0 if (x <= -3.2e-54) tmp = Float64(Float64(Float64(x + eps) - x) * Float64(x * 2.0)); 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 <= -3.2e-54) tmp = ((x + eps) - x) * (x * 2.0); else tmp = eps * (eps + (x * 2.0)); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -3.2e-54], N[(N[(N[(x + eps), $MachinePrecision] - x), $MachinePrecision] * N[(x * 2.0), $MachinePrecision]), $MachinePrecision], N[(eps * N[(eps + N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-54}:\\
\;\;\;\;\left(\left(x + \varepsilon\right) - x\right) \cdot \left(x \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(\varepsilon + x \cdot 2\right)\\
\end{array}
\end{array}
if x < -3.19999999999999998e-54Initial program 84.2%
unpow284.2%
unpow284.2%
difference-of-squares84.2%
+-commutative84.2%
+-commutative84.2%
Applied egg-rr84.2%
Taylor expanded in eps around 0 81.2%
if -3.19999999999999998e-54 < x Initial program 83.5%
+-commutative83.5%
unpow283.5%
unpow283.5%
difference-of-squares83.5%
*-commutative83.5%
associate--l+99.1%
+-inverses99.1%
+-rgt-identity99.1%
associate-+l+99.1%
*-lft-identity99.1%
metadata-eval99.1%
distribute-rgt-out99.1%
distribute-lft-out99.1%
metadata-eval99.1%
metadata-eval99.1%
metadata-eval99.1%
Simplified99.1%
Final simplification97.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 83.5%
+-commutative83.5%
unpow283.5%
unpow283.5%
difference-of-squares83.6%
*-commutative83.6%
associate--l+92.0%
+-inverses92.0%
+-rgt-identity92.0%
associate-+l+92.0%
*-lft-identity92.0%
metadata-eval92.0%
distribute-rgt-out92.0%
distribute-lft-out92.0%
metadata-eval92.0%
metadata-eval92.0%
metadata-eval92.0%
Simplified92.0%
Final simplification92.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 83.5%
+-commutative83.5%
unpow283.5%
unpow283.5%
difference-of-squares83.6%
*-commutative83.6%
associate--l+92.0%
+-inverses92.0%
+-rgt-identity92.0%
associate-+l+92.0%
*-lft-identity92.0%
metadata-eval92.0%
distribute-rgt-out92.0%
distribute-lft-out92.0%
metadata-eval92.0%
metadata-eval92.0%
metadata-eval92.0%
Simplified92.0%
Taylor expanded in eps around 0 55.0%
Final simplification55.0%
(FPCore (x eps) :precision binary64 (* x (* eps 2.0)))
double code(double x, double eps) {
return x * (eps * 2.0);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = x * (eps * 2.0d0)
end function
public static double code(double x, double eps) {
return x * (eps * 2.0);
}
def code(x, eps): return x * (eps * 2.0)
function code(x, eps) return Float64(x * Float64(eps * 2.0)) end
function tmp = code(x, eps) tmp = x * (eps * 2.0); end
code[x_, eps_] := N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(\varepsilon \cdot 2\right)
\end{array}
Initial program 83.5%
+-commutative83.5%
unpow283.5%
unpow283.5%
difference-of-squares83.6%
*-commutative83.6%
associate--l+92.0%
+-inverses92.0%
+-rgt-identity92.0%
associate-+l+92.0%
*-lft-identity92.0%
metadata-eval92.0%
distribute-rgt-out92.0%
distribute-lft-out92.0%
metadata-eval92.0%
metadata-eval92.0%
metadata-eval92.0%
Simplified92.0%
Taylor expanded in eps around 0 55.0%
associate-*r*55.0%
Simplified55.0%
Final simplification55.0%
herbie shell --seed 2024046
(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)))