
(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 (fma 2.0 x eps)))
double code(double x, double eps) {
return eps * fma(2.0, x, eps);
}
function code(x, eps) return Float64(eps * fma(2.0, x, eps)) end
code[x_, eps_] := N[(eps * N[(2.0 * x + eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \mathsf{fma}\left(2, x, \varepsilon\right)
\end{array}
Initial program 75.2%
unpow275.2%
unpow275.2%
difference-of-squares75.3%
*-commutative75.3%
+-commutative75.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -2.15e-110) (not (<= x 1e-114))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -2.15e-110) || !(x <= 1e-114)) {
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 <= (-2.15d-110)) .or. (.not. (x <= 1d-114))) 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 <= -2.15e-110) || !(x <= 1e-114)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -2.15e-110) or not (x <= 1e-114): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -2.15e-110) || !(x <= 1e-114)) 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 <= -2.15e-110) || ~((x <= 1e-114))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -2.15e-110], N[Not[LessEqual[x, 1e-114]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.15 \cdot 10^{-110} \lor \neg \left(x \leq 10^{-114}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -2.15000000000000012e-110 or 1.0000000000000001e-114 < x Initial program 36.3%
unpow236.3%
unpow236.3%
difference-of-squares36.3%
*-commutative36.3%
+-commutative36.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around 0 83.9%
if -2.15000000000000012e-110 < x < 1.0000000000000001e-114Initial program 98.6%
unpow298.6%
unpow298.6%
difference-of-squares98.7%
*-commutative98.7%
+-commutative98.7%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around inf 96.8%
unpow296.8%
Simplified96.8%
Final simplification91.9%
(FPCore (x eps) :precision binary64 (if (<= x -4.4e-110) (* 2.0 (* eps x)) (if (<= x 5e-115) (* eps eps) (* eps (+ x x)))))
double code(double x, double eps) {
double tmp;
if (x <= -4.4e-110) {
tmp = 2.0 * (eps * x);
} else if (x <= 5e-115) {
tmp = eps * eps;
} else {
tmp = eps * (x + x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-4.4d-110)) then
tmp = 2.0d0 * (eps * x)
else if (x <= 5d-115) then
tmp = eps * eps
else
tmp = eps * (x + x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -4.4e-110) {
tmp = 2.0 * (eps * x);
} else if (x <= 5e-115) {
tmp = eps * eps;
} else {
tmp = eps * (x + x);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -4.4e-110: tmp = 2.0 * (eps * x) elif x <= 5e-115: tmp = eps * eps else: tmp = eps * (x + x) return tmp
function code(x, eps) tmp = 0.0 if (x <= -4.4e-110) tmp = Float64(2.0 * Float64(eps * x)); elseif (x <= 5e-115) tmp = Float64(eps * eps); else tmp = Float64(eps * Float64(x + x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -4.4e-110) tmp = 2.0 * (eps * x); elseif (x <= 5e-115) tmp = eps * eps; else tmp = eps * (x + x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -4.4e-110], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 5e-115], N[(eps * eps), $MachinePrecision], N[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.4 \cdot 10^{-110}:\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{elif}\;x \leq 5 \cdot 10^{-115}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left(x + x\right)\\
\end{array}
\end{array}
if x < -4.3999999999999999e-110Initial program 36.2%
unpow236.2%
unpow236.2%
difference-of-squares36.2%
*-commutative36.2%
+-commutative36.2%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
+-commutative99.9%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around 0 84.7%
if -4.3999999999999999e-110 < x < 5.0000000000000003e-115Initial program 98.6%
unpow298.6%
unpow298.6%
difference-of-squares98.7%
*-commutative98.7%
+-commutative98.7%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around inf 96.8%
unpow296.8%
Simplified96.8%
if 5.0000000000000003e-115 < x Initial program 36.4%
unpow236.4%
unpow236.4%
difference-of-squares36.4%
*-commutative36.4%
+-commutative36.4%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
fma-udef100.0%
distribute-rgt-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around inf 83.0%
count-283.0%
distribute-lft-out83.0%
Simplified83.0%
Final simplification92.0%
(FPCore (x eps) :precision binary64 (+ (* eps (* 2.0 x)) (* eps eps)))
double code(double x, double eps) {
return (eps * (2.0 * x)) + (eps * eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (eps * (2.0d0 * x)) + (eps * eps)
end function
public static double code(double x, double eps) {
return (eps * (2.0 * x)) + (eps * eps);
}
def code(x, eps): return (eps * (2.0 * x)) + (eps * eps)
function code(x, eps) return Float64(Float64(eps * Float64(2.0 * x)) + Float64(eps * eps)) end
function tmp = code(x, eps) tmp = (eps * (2.0 * x)) + (eps * eps); end
code[x_, eps_] := N[(N[(eps * N[(2.0 * x), $MachinePrecision]), $MachinePrecision] + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(2 \cdot x\right) + \varepsilon \cdot \varepsilon
\end{array}
Initial program 75.2%
unpow275.2%
unpow275.2%
difference-of-squares75.3%
*-commutative75.3%
+-commutative75.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
fma-udef100.0%
distribute-rgt-in100.0%
Applied egg-rr100.0%
Final simplification100.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 75.2%
unpow275.2%
unpow275.2%
difference-of-squares75.3%
*-commutative75.3%
+-commutative75.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
Taylor expanded in eps around inf 72.2%
unpow272.2%
Simplified72.2%
Final simplification72.2%
herbie shell --seed 2023257
(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)))