
(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 4 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 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.5%
+-commutative82.5%
unpow282.5%
unpow282.5%
difference-of-squares82.6%
sub-neg82.6%
distribute-lft-in82.5%
+-commutative82.5%
distribute-lft-in82.6%
associate-+l+82.6%
remove-double-neg82.6%
sub-neg82.6%
+-commutative82.6%
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 (if (or (<= x -1.15e-62) (not (<= x 1.25e-102))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -1.15e-62) || !(x <= 1.25e-102)) {
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 <= (-1.15d-62)) .or. (.not. (x <= 1.25d-102))) 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 <= -1.15e-62) || !(x <= 1.25e-102)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -1.15e-62) or not (x <= 1.25e-102): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -1.15e-62) || !(x <= 1.25e-102)) 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 <= -1.15e-62) || ~((x <= 1.25e-102))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -1.15e-62], N[Not[LessEqual[x, 1.25e-102]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.15 \cdot 10^{-62} \lor \neg \left(x \leq 1.25 \cdot 10^{-102}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -1.15e-62 or 1.25000000000000006e-102 < x Initial program 39.4%
+-commutative39.4%
unpow239.4%
unpow239.4%
difference-of-squares39.5%
sub-neg39.5%
distribute-lft-in39.5%
+-commutative39.5%
distribute-lft-in39.5%
associate-+l+39.5%
remove-double-neg39.5%
sub-neg39.5%
+-commutative39.5%
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 88.3%
*-commutative88.3%
Simplified88.3%
if -1.15e-62 < x < 1.25000000000000006e-102Initial program 97.5%
+-commutative97.5%
unpow297.5%
unpow297.5%
difference-of-squares97.5%
sub-neg97.5%
distribute-lft-in97.5%
+-commutative97.5%
distribute-lft-in97.5%
associate-+l+97.5%
remove-double-neg97.5%
sub-neg97.5%
+-commutative97.5%
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.4%
Final simplification94.3%
(FPCore (x eps) :precision binary64 (if (<= x -1.15e-62) (* 2.0 (* eps x)) (if (<= x 1e-103) (* eps eps) (* x (* eps 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.15e-62) {
tmp = 2.0 * (eps * x);
} else if (x <= 1e-103) {
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.15d-62)) then
tmp = 2.0d0 * (eps * x)
else if (x <= 1d-103) 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.15e-62) {
tmp = 2.0 * (eps * x);
} else if (x <= 1e-103) {
tmp = eps * eps;
} else {
tmp = x * (eps * 2.0);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.15e-62: tmp = 2.0 * (eps * x) elif x <= 1e-103: tmp = eps * eps else: tmp = x * (eps * 2.0) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.15e-62) tmp = Float64(2.0 * Float64(eps * x)); elseif (x <= 1e-103) 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.15e-62) tmp = 2.0 * (eps * x); elseif (x <= 1e-103) tmp = eps * eps; else tmp = x * (eps * 2.0); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.15e-62], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1e-103], N[(eps * eps), $MachinePrecision], N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.15 \cdot 10^{-62}:\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{elif}\;x \leq 10^{-103}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(\varepsilon \cdot 2\right)\\
\end{array}
\end{array}
if x < -1.15e-62Initial program 41.3%
+-commutative41.3%
unpow241.3%
unpow241.3%
difference-of-squares41.3%
sub-neg41.3%
distribute-lft-in41.3%
+-commutative41.3%
distribute-lft-in41.3%
associate-+l+41.3%
remove-double-neg41.3%
sub-neg41.3%
+-commutative41.3%
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 87.4%
*-commutative87.4%
Simplified87.4%
if -1.15e-62 < x < 9.99999999999999958e-104Initial program 97.5%
+-commutative97.5%
unpow297.5%
unpow297.5%
difference-of-squares97.5%
sub-neg97.5%
distribute-lft-in97.5%
+-commutative97.5%
distribute-lft-in97.5%
associate-+l+97.5%
remove-double-neg97.5%
sub-neg97.5%
+-commutative97.5%
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.4%
if 9.99999999999999958e-104 < x Initial program 38.5%
+-commutative38.5%
unpow238.5%
unpow238.5%
difference-of-squares38.7%
sub-neg38.7%
distribute-lft-in38.7%
+-commutative38.7%
distribute-lft-in38.7%
associate-+l+38.7%
remove-double-neg38.7%
sub-neg38.7%
+-commutative38.7%
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 88.7%
*-commutative88.7%
Simplified88.7%
Taylor expanded in x around 0 88.7%
associate-*r*88.7%
Simplified88.7%
Final simplification94.3%
(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 82.5%
+-commutative82.5%
unpow282.5%
unpow282.5%
difference-of-squares82.6%
sub-neg82.6%
distribute-lft-in82.5%
+-commutative82.5%
distribute-lft-in82.6%
associate-+l+82.6%
remove-double-neg82.6%
sub-neg82.6%
+-commutative82.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 79.7%
herbie shell --seed 2024177
(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)))