
(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 eps eps (* x (* eps 2.0))))
double code(double x, double eps) {
return fma(eps, eps, (x * (eps * 2.0)));
}
function code(x, eps) return fma(eps, eps, Float64(x * Float64(eps * 2.0))) end
code[x_, eps_] := N[(eps * eps + N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, \varepsilon, x \cdot \left(\varepsilon \cdot 2\right)\right)
\end{array}
Initial program 78.6%
+-commutative78.6%
unpow278.6%
unpow278.6%
difference-of-squares78.6%
sub-neg78.6%
distribute-lft-in78.6%
+-commutative78.6%
distribute-lft-in78.6%
+-commutative78.6%
sub-neg78.6%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
*-commutative99.9%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
distribute-rgt-in99.9%
fma-define100.0%
associate-*l*100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -6.2e-101) (not (<= x 4.2e-85))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -6.2e-101) || !(x <= 4.2e-85)) {
tmp = 2.0 * (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 ((x <= (-6.2d-101)) .or. (.not. (x <= 4.2d-85))) then
tmp = 2.0d0 * (eps * x)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -6.2e-101) || !(x <= 4.2e-85)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -6.2e-101) or not (x <= 4.2e-85): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -6.2e-101) || !(x <= 4.2e-85)) tmp = Float64(2.0 * Float64(eps * x)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -6.2e-101) || ~((x <= 4.2e-85))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -6.2e-101], N[Not[LessEqual[x, 4.2e-85]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.2 \cdot 10^{-101} \lor \neg \left(x \leq 4.2 \cdot 10^{-85}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -6.19999999999999946e-101 or 4.2e-85 < x Initial program 37.5%
+-commutative37.5%
unpow237.5%
unpow237.5%
difference-of-squares37.6%
sub-neg37.6%
distribute-lft-in37.5%
+-commutative37.5%
distribute-lft-in37.6%
+-commutative37.6%
sub-neg37.6%
associate--l+99.8%
+-inverses99.8%
+-rgt-identity99.8%
*-commutative99.8%
associate-+l+99.9%
count-299.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in eps around 0 82.5%
*-commutative82.5%
Simplified82.5%
if -6.19999999999999946e-101 < x < 4.2e-85Initial program 98.0%
+-commutative98.0%
unpow298.0%
unpow298.0%
difference-of-squares98.0%
sub-neg98.0%
distribute-lft-in97.9%
+-commutative97.9%
distribute-lft-in98.0%
+-commutative98.0%
sub-neg98.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 inf 95.8%
Final simplification91.5%
(FPCore (x eps) :precision binary64 (if (<= x -2.1e-98) (* x (* eps 2.0)) (if (<= x 2.3e-84) (* eps eps) (* 2.0 (* eps x)))))
double code(double x, double eps) {
double tmp;
if (x <= -2.1e-98) {
tmp = x * (eps * 2.0);
} else if (x <= 2.3e-84) {
tmp = eps * eps;
} else {
tmp = 2.0 * (eps * x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-2.1d-98)) then
tmp = x * (eps * 2.0d0)
else if (x <= 2.3d-84) then
tmp = eps * eps
else
tmp = 2.0d0 * (eps * x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -2.1e-98) {
tmp = x * (eps * 2.0);
} else if (x <= 2.3e-84) {
tmp = eps * eps;
} else {
tmp = 2.0 * (eps * x);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -2.1e-98: tmp = x * (eps * 2.0) elif x <= 2.3e-84: tmp = eps * eps else: tmp = 2.0 * (eps * x) return tmp
function code(x, eps) tmp = 0.0 if (x <= -2.1e-98) tmp = Float64(x * Float64(eps * 2.0)); elseif (x <= 2.3e-84) tmp = Float64(eps * eps); else tmp = Float64(2.0 * Float64(eps * x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -2.1e-98) tmp = x * (eps * 2.0); elseif (x <= 2.3e-84) tmp = eps * eps; else tmp = 2.0 * (eps * x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -2.1e-98], N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.3e-84], N[(eps * eps), $MachinePrecision], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.1 \cdot 10^{-98}:\\
\;\;\;\;x \cdot \left(\varepsilon \cdot 2\right)\\
\mathbf{elif}\;x \leq 2.3 \cdot 10^{-84}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\end{array}
\end{array}
if x < -2.09999999999999992e-98Initial program 42.6%
+-commutative42.6%
unpow242.6%
unpow242.6%
difference-of-squares42.6%
sub-neg42.6%
distribute-lft-in42.5%
+-commutative42.5%
distribute-lft-in42.6%
+-commutative42.6%
sub-neg42.6%
associate--l+99.8%
+-inverses99.8%
+-rgt-identity99.8%
*-commutative99.8%
associate-+l+99.9%
count-299.9%
*-commutative99.9%
Simplified99.9%
distribute-rgt-in99.9%
fma-define99.9%
associate-*l*99.9%
Applied egg-rr99.9%
Taylor expanded in eps around 0 80.0%
*-commutative80.0%
associate-*r*80.0%
*-commutative80.0%
associate-*r*80.0%
Simplified80.0%
if -2.09999999999999992e-98 < x < 2.29999999999999981e-84Initial program 98.0%
+-commutative98.0%
unpow298.0%
unpow298.0%
difference-of-squares98.0%
sub-neg98.0%
distribute-lft-in97.9%
+-commutative97.9%
distribute-lft-in98.0%
+-commutative98.0%
sub-neg98.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 inf 95.8%
if 2.29999999999999981e-84 < x Initial program 30.4%
+-commutative30.4%
unpow230.4%
unpow230.4%
difference-of-squares30.4%
sub-neg30.4%
distribute-lft-in30.3%
+-commutative30.3%
distribute-lft-in30.4%
+-commutative30.4%
sub-neg30.4%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
*-commutative99.9%
associate-+l+99.9%
count-299.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in eps around 0 86.1%
*-commutative86.1%
Simplified86.1%
Final simplification91.6%
(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 78.6%
+-commutative78.6%
unpow278.6%
unpow278.6%
difference-of-squares78.6%
sub-neg78.6%
distribute-lft-in78.6%
+-commutative78.6%
distribute-lft-in78.6%
+-commutative78.6%
sub-neg78.6%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
*-commutative99.9%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
(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 78.6%
+-commutative78.6%
unpow278.6%
unpow278.6%
difference-of-squares78.6%
sub-neg78.6%
distribute-lft-in78.6%
+-commutative78.6%
distribute-lft-in78.6%
+-commutative78.6%
sub-neg78.6%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
*-commutative99.9%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in eps around inf 73.9%
herbie shell --seed 2024132
(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)))