
(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 6 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 -4.8e-91) (- (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 <= -4.8e-91) {
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 <= -4.8e-91) 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, -4.8e-91], 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 -4.8 \cdot 10^{-91}:\\
\;\;\;\;{\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 < -4.80000000000000022e-91Initial program 90.7%
if -4.80000000000000022e-91 < x Initial program 84.4%
+-commutative84.4%
sqr-pow84.4%
sqr-pow84.4%
unpow284.4%
unpow284.4%
difference-of-squares84.4%
associate--l+99.3%
+-inverses99.3%
+-rgt-identity99.3%
*-commutative99.3%
associate-+l+99.3%
*-lft-identity99.3%
metadata-eval99.3%
distribute-rgt-out99.3%
distribute-lft-out99.3%
metadata-eval99.3%
metadata-eval99.3%
metadata-eval99.3%
Simplified99.3%
distribute-lft-in99.3%
fma-def99.3%
Applied egg-rr99.3%
Final simplification97.8%
(FPCore (x eps) :precision binary64 (if (<= x -9e-91) (* (+ x (+ x eps)) (- (+ x eps) x)) (fma eps eps (* eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -9e-91) {
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 <= -9e-91) 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, -9e-91], 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 -9 \cdot 10^{-91}:\\
\;\;\;\;\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 < -8.99999999999999952e-91Initial program 90.7%
unpow290.7%
unpow290.7%
difference-of-squares90.6%
+-commutative90.6%
+-commutative90.6%
Applied egg-rr90.6%
if -8.99999999999999952e-91 < x Initial program 84.4%
+-commutative84.4%
sqr-pow84.4%
sqr-pow84.4%
unpow284.4%
unpow284.4%
difference-of-squares84.4%
associate--l+99.3%
+-inverses99.3%
+-rgt-identity99.3%
*-commutative99.3%
associate-+l+99.3%
*-lft-identity99.3%
metadata-eval99.3%
distribute-rgt-out99.3%
distribute-lft-out99.3%
metadata-eval99.3%
metadata-eval99.3%
metadata-eval99.3%
Simplified99.3%
distribute-lft-in99.3%
fma-def99.3%
Applied egg-rr99.3%
Final simplification97.8%
(FPCore (x eps) :precision binary64 (if (<= x -5.5e-91) (* (+ x (+ x eps)) (- (+ x eps) x)) (* eps (+ eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -5.5e-91) {
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 <= (-5.5d-91)) 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 <= -5.5e-91) {
tmp = (x + (x + eps)) * ((x + eps) - x);
} else {
tmp = eps * (eps + (x * 2.0));
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -5.5e-91: 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 <= -5.5e-91) 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 <= -5.5e-91) 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, -5.5e-91], 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 -5.5 \cdot 10^{-91}:\\
\;\;\;\;\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 < -5.49999999999999965e-91Initial program 90.7%
unpow290.7%
unpow290.7%
difference-of-squares90.6%
+-commutative90.6%
+-commutative90.6%
Applied egg-rr90.6%
if -5.49999999999999965e-91 < x Initial program 84.4%
+-commutative84.4%
sqr-pow84.4%
sqr-pow84.4%
unpow284.4%
unpow284.4%
difference-of-squares84.4%
associate--l+99.3%
+-inverses99.3%
+-rgt-identity99.3%
*-commutative99.3%
associate-+l+99.3%
*-lft-identity99.3%
metadata-eval99.3%
distribute-rgt-out99.3%
distribute-lft-out99.3%
metadata-eval99.3%
metadata-eval99.3%
metadata-eval99.3%
Simplified99.3%
Final simplification97.8%
(FPCore (x eps) :precision binary64 (if (<= x -1.35e-90) (* (* x 2.0) (- (+ x eps) x)) (* eps (+ eps (* x 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.35e-90) {
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.35d-90)) 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.35e-90) {
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.35e-90: 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.35e-90) 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.35e-90) 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.35e-90], 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.35 \cdot 10^{-90}:\\
\;\;\;\;\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.34999999999999998e-90Initial program 90.7%
unpow290.7%
unpow290.7%
difference-of-squares90.6%
+-commutative90.6%
+-commutative90.6%
Applied egg-rr90.6%
Taylor expanded in eps around 0 75.6%
if -1.34999999999999998e-90 < x Initial program 84.4%
+-commutative84.4%
sqr-pow84.4%
sqr-pow84.4%
unpow284.4%
unpow284.4%
difference-of-squares84.4%
associate--l+99.3%
+-inverses99.3%
+-rgt-identity99.3%
*-commutative99.3%
associate-+l+99.3%
*-lft-identity99.3%
metadata-eval99.3%
distribute-rgt-out99.3%
distribute-lft-out99.3%
metadata-eval99.3%
metadata-eval99.3%
metadata-eval99.3%
Simplified99.3%
Final simplification95.2%
(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 85.5%
+-commutative85.5%
sqr-pow85.5%
sqr-pow85.5%
unpow285.5%
unpow285.5%
difference-of-squares85.5%
associate--l+89.7%
+-inverses89.7%
+-rgt-identity89.7%
*-commutative89.7%
associate-+l+89.7%
*-lft-identity89.7%
metadata-eval89.7%
distribute-rgt-out89.7%
distribute-lft-out89.7%
metadata-eval89.7%
metadata-eval89.7%
metadata-eval89.7%
Simplified89.7%
Final simplification89.7%
(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 85.5%
+-commutative85.5%
sqr-pow85.5%
sqr-pow85.5%
unpow285.5%
unpow285.5%
difference-of-squares85.5%
associate--l+89.7%
+-inverses89.7%
+-rgt-identity89.7%
*-commutative89.7%
associate-+l+89.7%
*-lft-identity89.7%
metadata-eval89.7%
distribute-rgt-out89.7%
distribute-lft-out89.7%
metadata-eval89.7%
metadata-eval89.7%
metadata-eval89.7%
Simplified89.7%
Taylor expanded in eps around 0 52.6%
Final simplification52.6%
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)))