
(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 (+ (* eps eps) (* x (* eps 2.0))))
double code(double x, double eps) {
return (eps * eps) + (x * (eps * 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (eps * eps) + (x * (eps * 2.0d0))
end function
public static double code(double x, double eps) {
return (eps * eps) + (x * (eps * 2.0));
}
def code(x, eps): return (eps * eps) + (x * (eps * 2.0))
function code(x, eps) return Float64(Float64(eps * eps) + Float64(x * Float64(eps * 2.0))) end
function tmp = code(x, eps) tmp = (eps * eps) + (x * (eps * 2.0)); end
code[x_, eps_] := N[(N[(eps * eps), $MachinePrecision] + N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \varepsilon + x \cdot \left(\varepsilon \cdot 2\right)
\end{array}
Initial program 73.0%
+-commutative73.0%
unpow273.0%
unpow273.0%
difference-of-squares73.0%
sub-neg73.0%
distribute-lft-in73.0%
+-commutative73.0%
distribute-lft-in73.0%
+-commutative73.0%
sub-neg73.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
distribute-rgt-in100.0%
pow2100.0%
associate-*l*100.0%
Applied egg-rr100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -3.8e-102) (not (<= x 9e-106))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -3.8e-102) || !(x <= 9e-106)) {
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 <= (-3.8d-102)) .or. (.not. (x <= 9d-106))) 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 <= -3.8e-102) || !(x <= 9e-106)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -3.8e-102) or not (x <= 9e-106): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -3.8e-102) || !(x <= 9e-106)) 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 <= -3.8e-102) || ~((x <= 9e-106))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -3.8e-102], N[Not[LessEqual[x, 9e-106]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.8 \cdot 10^{-102} \lor \neg \left(x \leq 9 \cdot 10^{-106}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -3.80000000000000026e-102 or 8.99999999999999911e-106 < x Initial program 31.3%
+-commutative31.3%
unpow231.3%
unpow231.3%
difference-of-squares31.4%
sub-neg31.4%
distribute-lft-in31.2%
+-commutative31.2%
distribute-lft-in31.4%
+-commutative31.4%
sub-neg31.4%
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 85.4%
*-commutative85.4%
Simplified85.4%
if -3.80000000000000026e-102 < x < 8.99999999999999911e-106Initial program 97.6%
+-commutative97.6%
unpow297.6%
unpow297.6%
difference-of-squares97.6%
sub-neg97.6%
distribute-lft-in97.6%
+-commutative97.6%
distribute-lft-in97.6%
+-commutative97.6%
sub-neg97.6%
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 96.0%
Final simplification92.0%
(FPCore (x eps) :precision binary64 (if (<= x -1.22e-102) (* 2.0 (* eps x)) (if (<= x 1.4e-106) (* eps eps) (* x (* eps 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.22e-102) {
tmp = 2.0 * (eps * x);
} else if (x <= 1.4e-106) {
tmp = eps * eps;
} else {
tmp = x * (eps * 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.22d-102)) then
tmp = 2.0d0 * (eps * x)
else if (x <= 1.4d-106) then
tmp = eps * eps
else
tmp = x * (eps * 2.0d0)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -1.22e-102) {
tmp = 2.0 * (eps * x);
} else if (x <= 1.4e-106) {
tmp = eps * eps;
} else {
tmp = x * (eps * 2.0);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.22e-102: tmp = 2.0 * (eps * x) elif x <= 1.4e-106: tmp = eps * eps else: tmp = x * (eps * 2.0) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.22e-102) tmp = Float64(2.0 * Float64(eps * x)); elseif (x <= 1.4e-106) tmp = Float64(eps * eps); else tmp = Float64(x * Float64(eps * 2.0)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -1.22e-102) tmp = 2.0 * (eps * x); elseif (x <= 1.4e-106) tmp = eps * eps; else tmp = x * (eps * 2.0); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.22e-102], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.4e-106], N[(eps * eps), $MachinePrecision], N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.22 \cdot 10^{-102}:\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{elif}\;x \leq 1.4 \cdot 10^{-106}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(\varepsilon \cdot 2\right)\\
\end{array}
\end{array}
if x < -1.22e-102Initial program 34.2%
+-commutative34.2%
unpow234.2%
unpow234.2%
difference-of-squares34.1%
sub-neg34.1%
distribute-lft-in34.0%
+-commutative34.0%
distribute-lft-in34.1%
+-commutative34.1%
sub-neg34.1%
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 81.2%
*-commutative81.2%
Simplified81.2%
if -1.22e-102 < x < 1.39999999999999994e-106Initial program 97.6%
+-commutative97.6%
unpow297.6%
unpow297.6%
difference-of-squares97.6%
sub-neg97.6%
distribute-lft-in97.6%
+-commutative97.6%
distribute-lft-in97.6%
+-commutative97.6%
sub-neg97.6%
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 96.0%
if 1.39999999999999994e-106 < x Initial program 28.3%
+-commutative28.3%
unpow228.3%
unpow228.3%
difference-of-squares28.4%
sub-neg28.4%
distribute-lft-in28.2%
+-commutative28.2%
distribute-lft-in28.4%
+-commutative28.4%
sub-neg28.4%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
distribute-rgt-in100.0%
pow2100.0%
associate-*l*100.0%
Applied egg-rr100.0%
Taylor expanded in eps around 0 89.8%
associate-*r*89.8%
Simplified89.8%
Final simplification92.0%
(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 73.0%
+-commutative73.0%
unpow273.0%
unpow273.0%
difference-of-squares73.0%
sub-neg73.0%
distribute-lft-in73.0%
+-commutative73.0%
distribute-lft-in73.0%
+-commutative73.0%
sub-neg73.0%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
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 73.0%
+-commutative73.0%
unpow273.0%
unpow273.0%
difference-of-squares73.0%
sub-neg73.0%
distribute-lft-in73.0%
+-commutative73.0%
distribute-lft-in73.0%
+-commutative73.0%
sub-neg73.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 69.9%
herbie shell --seed 2024146
(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)))