
(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 (+ x x) eps (* eps eps)))
double code(double x, double eps) {
return fma((x + x), eps, (eps * eps));
}
function code(x, eps) return fma(Float64(x + x), eps, Float64(eps * eps)) end
code[x_, eps_] := N[(N[(x + x), $MachinePrecision] * eps + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x + x, \varepsilon, \varepsilon \cdot \varepsilon\right)
\end{array}
Initial program 73.6%
unpow273.6%
unpow273.6%
difference-of-squares73.6%
*-commutative73.6%
+-commutative73.6%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+100.0%
count-2100.0%
fma-def100.0%
Simplified100.0%
add-cbrt-cube59.6%
pow359.6%
Applied egg-rr59.6%
rem-cbrt-cube100.0%
fma-udef100.0%
distribute-rgt-out100.0%
fma-def100.0%
add-log-exp73.1%
*-commutative73.1%
exp-lft-sqr73.1%
log-prod73.1%
add-log-exp75.9%
add-log-exp100.0%
Applied egg-rr100.0%
Final simplification100.0%
(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 73.6%
unpow273.6%
unpow273.6%
difference-of-squares73.6%
*-commutative73.6%
+-commutative73.6%
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 -4.3e-110) (not (<= x 2.7e-100))) (* eps (* x 2.0)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -4.3e-110) || !(x <= 2.7e-100)) {
tmp = eps * (x * 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 <= (-4.3d-110)) .or. (.not. (x <= 2.7d-100))) then
tmp = eps * (x * 2.0d0)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -4.3e-110) || !(x <= 2.7e-100)) {
tmp = eps * (x * 2.0);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -4.3e-110) or not (x <= 2.7e-100): tmp = eps * (x * 2.0) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -4.3e-110) || !(x <= 2.7e-100)) tmp = Float64(eps * Float64(x * 2.0)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -4.3e-110) || ~((x <= 2.7e-100))) tmp = eps * (x * 2.0); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -4.3e-110], N[Not[LessEqual[x, 2.7e-100]], $MachinePrecision]], N[(eps * N[(x * 2.0), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.3 \cdot 10^{-110} \lor \neg \left(x \leq 2.7 \cdot 10^{-100}\right):\\
\;\;\;\;\varepsilon \cdot \left(x \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -4.30000000000000025e-110 or 2.70000000000000016e-100 < x Initial program 38.6%
unpow238.6%
unpow238.6%
difference-of-squares38.8%
*-commutative38.8%
+-commutative38.8%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
+-commutative100.0%
associate-+r+99.9%
count-299.9%
fma-def99.9%
Simplified99.9%
Taylor expanded in eps around 0 85.0%
*-commutative85.0%
associate-*l*85.1%
*-commutative85.1%
Simplified85.1%
if -4.30000000000000025e-110 < x < 2.70000000000000016e-100Initial program 97.1%
unpow297.1%
unpow297.1%
difference-of-squares97.1%
*-commutative97.1%
+-commutative97.1%
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.5%
unpow296.5%
Simplified96.5%
Final simplification91.9%
(FPCore (x eps) :precision binary64 (+ (* eps eps) (* eps (* x 2.0))))
double code(double x, double eps) {
return (eps * eps) + (eps * (x * 2.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (eps * eps) + (eps * (x * 2.0d0))
end function
public static double code(double x, double eps) {
return (eps * eps) + (eps * (x * 2.0));
}
def code(x, eps): return (eps * eps) + (eps * (x * 2.0))
function code(x, eps) return Float64(Float64(eps * eps) + Float64(eps * Float64(x * 2.0))) end
function tmp = code(x, eps) tmp = (eps * eps) + (eps * (x * 2.0)); end
code[x_, eps_] := N[(N[(eps * eps), $MachinePrecision] + N[(eps * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \varepsilon + \varepsilon \cdot \left(x \cdot 2\right)
\end{array}
Initial program 73.6%
unpow273.6%
unpow273.6%
difference-of-squares73.6%
*-commutative73.6%
+-commutative73.6%
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 73.6%
unpow273.6%
unpow273.6%
difference-of-squares73.6%
*-commutative73.6%
+-commutative73.6%
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 70.5%
unpow270.5%
Simplified70.5%
Final simplification70.5%
herbie shell --seed 2023187
(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)))