
(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 76.9%
+-commutative76.9%
unpow276.9%
unpow276.9%
difference-of-squares77.0%
sub-neg77.0%
distribute-lft-in76.9%
+-commutative76.9%
distribute-lft-in77.0%
associate-+l+76.9%
remove-double-neg76.9%
sub-neg76.9%
+-commutative76.9%
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 -9.5e-136) (not (<= x 3.65e-106))) (* x (* eps 2.0)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -9.5e-136) || !(x <= 3.65e-106)) {
tmp = x * (eps * 2.0);
} 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 <= (-9.5d-136)) .or. (.not. (x <= 3.65d-106))) then
tmp = x * (eps * 2.0d0)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -9.5e-136) || !(x <= 3.65e-106)) {
tmp = x * (eps * 2.0);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -9.5e-136) or not (x <= 3.65e-106): tmp = x * (eps * 2.0) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -9.5e-136) || !(x <= 3.65e-106)) tmp = Float64(x * Float64(eps * 2.0)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -9.5e-136) || ~((x <= 3.65e-106))) tmp = x * (eps * 2.0); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -9.5e-136], N[Not[LessEqual[x, 3.65e-106]], $MachinePrecision]], N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.5 \cdot 10^{-136} \lor \neg \left(x \leq 3.65 \cdot 10^{-106}\right):\\
\;\;\;\;x \cdot \left(\varepsilon \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -9.5000000000000007e-136 or 3.64999999999999996e-106 < x Initial program 40.8%
+-commutative40.8%
unpow240.8%
unpow240.8%
difference-of-squares41.0%
sub-neg41.0%
distribute-lft-in40.9%
+-commutative40.9%
distribute-lft-in41.0%
associate-+l+40.9%
remove-double-neg40.9%
sub-neg40.9%
+-commutative40.9%
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 x around inf 99.9%
*-commutative99.9%
unpow299.9%
associate-/l*99.9%
distribute-lft-out99.9%
Simplified99.9%
Taylor expanded in eps around 0 85.3%
if -9.5000000000000007e-136 < x < 3.64999999999999996e-106Initial program 99.3%
+-commutative99.3%
unpow299.3%
unpow299.3%
difference-of-squares99.3%
sub-neg99.3%
distribute-lft-in99.3%
+-commutative99.3%
distribute-lft-in99.3%
associate-+l+99.3%
remove-double-neg99.3%
sub-neg99.3%
+-commutative99.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 inf 98.6%
Final simplification93.5%
(FPCore (x eps) :precision binary64 (if (or (<= x -9e-136) (not (<= x 4.3e-107))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -9e-136) || !(x <= 4.3e-107)) {
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 <= (-9d-136)) .or. (.not. (x <= 4.3d-107))) 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 <= -9e-136) || !(x <= 4.3e-107)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -9e-136) or not (x <= 4.3e-107): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -9e-136) || !(x <= 4.3e-107)) 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 <= -9e-136) || ~((x <= 4.3e-107))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -9e-136], N[Not[LessEqual[x, 4.3e-107]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9 \cdot 10^{-136} \lor \neg \left(x \leq 4.3 \cdot 10^{-107}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -8.99999999999999944e-136 or 4.2999999999999997e-107 < x Initial program 40.8%
+-commutative40.8%
unpow240.8%
unpow240.8%
difference-of-squares41.0%
sub-neg41.0%
distribute-lft-in40.9%
+-commutative40.9%
distribute-lft-in41.0%
associate-+l+40.9%
remove-double-neg40.9%
sub-neg40.9%
+-commutative40.9%
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.2%
*-commutative85.2%
Simplified85.2%
if -8.99999999999999944e-136 < x < 4.2999999999999997e-107Initial program 99.3%
+-commutative99.3%
unpow299.3%
unpow299.3%
difference-of-squares99.3%
sub-neg99.3%
distribute-lft-in99.3%
+-commutative99.3%
distribute-lft-in99.3%
associate-+l+99.3%
remove-double-neg99.3%
sub-neg99.3%
+-commutative99.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 inf 98.6%
Final simplification93.5%
(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 76.9%
+-commutative76.9%
unpow276.9%
unpow276.9%
difference-of-squares77.0%
sub-neg77.0%
distribute-lft-in76.9%
+-commutative76.9%
distribute-lft-in77.0%
associate-+l+76.9%
remove-double-neg76.9%
sub-neg76.9%
+-commutative76.9%
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 74.9%
herbie shell --seed 2024160
(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)))