
(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 7 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 2.0) eps (* eps eps)))
double code(double x, double eps) {
return fma((x * 2.0), eps, (eps * eps));
}
function code(x, eps) return fma(Float64(x * 2.0), eps, Float64(eps * eps)) end
code[x_, eps_] := N[(N[(x * 2.0), $MachinePrecision] * eps + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x \cdot 2, \varepsilon, \varepsilon \cdot \varepsilon\right)
\end{array}
Initial program 72.3%
+-commutative72.3%
unpow272.3%
unpow272.3%
difference-of-squares72.3%
sub-neg72.3%
distribute-lft-in72.3%
+-commutative72.3%
distribute-lft-in72.3%
associate-+l+72.3%
remove-double-neg72.3%
sub-neg72.3%
+-commutative72.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
+-commutative100.0%
distribute-rgt-in100.0%
fma-define100.0%
pow2100.0%
Applied egg-rr100.0%
pow2100.0%
Applied egg-rr100.0%
(FPCore (x eps) :precision binary64 (fma eps eps (* x (* 2.0 eps))))
double code(double x, double eps) {
return fma(eps, eps, (x * (2.0 * eps)));
}
function code(x, eps) return fma(eps, eps, Float64(x * Float64(2.0 * eps))) end
code[x_, eps_] := N[(eps * eps + N[(x * N[(2.0 * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, \varepsilon, x \cdot \left(2 \cdot \varepsilon\right)\right)
\end{array}
Initial program 72.3%
+-commutative72.3%
unpow272.3%
unpow272.3%
difference-of-squares72.3%
sub-neg72.3%
distribute-lft-in72.3%
+-commutative72.3%
distribute-lft-in72.3%
associate-+l+72.3%
remove-double-neg72.3%
sub-neg72.3%
+-commutative72.3%
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%
fma-define100.0%
associate-*l*100.0%
Applied egg-rr100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -3.4e-130) (not (<= x 7.5e-120))) (* eps (+ x x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -3.4e-130) || !(x <= 7.5e-120)) {
tmp = eps * (x + 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.4d-130)) .or. (.not. (x <= 7.5d-120))) then
tmp = eps * (x + x)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -3.4e-130) || !(x <= 7.5e-120)) {
tmp = eps * (x + x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -3.4e-130) or not (x <= 7.5e-120): tmp = eps * (x + x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -3.4e-130) || !(x <= 7.5e-120)) tmp = Float64(eps * Float64(x + x)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -3.4e-130) || ~((x <= 7.5e-120))) tmp = eps * (x + x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -3.4e-130], N[Not[LessEqual[x, 7.5e-120]], $MachinePrecision]], N[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.4 \cdot 10^{-130} \lor \neg \left(x \leq 7.5 \cdot 10^{-120}\right):\\
\;\;\;\;\varepsilon \cdot \left(x + x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -3.40000000000000005e-130 or 7.5000000000000004e-120 < x Initial program 39.9%
sub-neg39.9%
Applied egg-rr39.9%
sub-neg39.9%
unpow239.8%
unpow239.9%
difference-of-squares39.9%
*-commutative39.9%
+-commutative39.9%
+-commutative39.9%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
Simplified99.9%
Taylor expanded in x around inf 83.9%
if -3.40000000000000005e-130 < x < 7.5000000000000004e-120Initial program 98.8%
+-commutative98.8%
unpow298.8%
unpow298.8%
difference-of-squares98.8%
sub-neg98.8%
distribute-lft-in98.8%
+-commutative98.8%
distribute-lft-in98.8%
associate-+l+98.8%
remove-double-neg98.8%
sub-neg98.8%
+-commutative98.8%
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.0%
Final simplification91.7%
(FPCore (x eps) :precision binary64 (if (or (<= x -3.2e-130) (not (<= x 8e-120))) (* 2.0 (* x eps)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -3.2e-130) || !(x <= 8e-120)) {
tmp = 2.0 * (x * eps);
} 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.2d-130)) .or. (.not. (x <= 8d-120))) then
tmp = 2.0d0 * (x * eps)
else
tmp = eps * eps
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if ((x <= -3.2e-130) || !(x <= 8e-120)) {
tmp = 2.0 * (x * eps);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -3.2e-130) or not (x <= 8e-120): tmp = 2.0 * (x * eps) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -3.2e-130) || !(x <= 8e-120)) tmp = Float64(2.0 * Float64(x * eps)); else tmp = Float64(eps * eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if ((x <= -3.2e-130) || ~((x <= 8e-120))) tmp = 2.0 * (x * eps); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -3.2e-130], N[Not[LessEqual[x, 8e-120]], $MachinePrecision]], N[(2.0 * N[(x * eps), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-130} \lor \neg \left(x \leq 8 \cdot 10^{-120}\right):\\
\;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -3.2e-130 or 7.99999999999999983e-120 < x Initial program 39.9%
+-commutative39.9%
unpow239.9%
unpow239.9%
difference-of-squares39.9%
sub-neg39.9%
distribute-lft-in39.8%
+-commutative39.8%
distribute-lft-in39.9%
associate-+l+39.9%
remove-double-neg39.9%
sub-neg39.9%
+-commutative39.9%
associate--l+99.9%
+-inverses99.9%
+-rgt-identity99.9%
*-commutative99.9%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in eps around 0 83.9%
*-commutative83.9%
Simplified83.9%
if -3.2e-130 < x < 7.99999999999999983e-120Initial program 98.8%
+-commutative98.8%
unpow298.8%
unpow298.8%
difference-of-squares98.8%
sub-neg98.8%
distribute-lft-in98.8%
+-commutative98.8%
distribute-lft-in98.8%
associate-+l+98.8%
remove-double-neg98.8%
sub-neg98.8%
+-commutative98.8%
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.0%
Final simplification91.6%
(FPCore (x eps) :precision binary64 (* eps (+ (* x 2.0) eps)))
double code(double x, double eps) {
return eps * ((x * 2.0) + eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps * ((x * 2.0d0) + eps)
end function
public static double code(double x, double eps) {
return eps * ((x * 2.0) + eps);
}
def code(x, eps): return eps * ((x * 2.0) + eps)
function code(x, eps) return Float64(eps * Float64(Float64(x * 2.0) + eps)) end
function tmp = code(x, eps) tmp = eps * ((x * 2.0) + eps); end
code[x_, eps_] := N[(eps * N[(N[(x * 2.0), $MachinePrecision] + eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \left(x \cdot 2 + \varepsilon\right)
\end{array}
Initial program 72.3%
+-commutative72.3%
unpow272.3%
unpow272.3%
difference-of-squares72.3%
sub-neg72.3%
distribute-lft-in72.3%
+-commutative72.3%
distribute-lft-in72.3%
associate-+l+72.3%
remove-double-neg72.3%
sub-neg72.3%
+-commutative72.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
*-commutative100.0%
associate-+l+100.0%
count-2100.0%
*-commutative100.0%
Simplified100.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 72.3%
+-commutative72.3%
unpow272.3%
unpow272.3%
difference-of-squares72.3%
sub-neg72.3%
distribute-lft-in72.3%
+-commutative72.3%
distribute-lft-in72.3%
associate-+l+72.3%
remove-double-neg72.3%
sub-neg72.3%
+-commutative72.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 69.8%
(FPCore (x eps) :precision binary64 -8.0)
double code(double x, double eps) {
return -8.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = -8.0d0
end function
public static double code(double x, double eps) {
return -8.0;
}
def code(x, eps): return -8.0
function code(x, eps) return -8.0 end
function tmp = code(x, eps) tmp = -8.0; end
code[x_, eps_] := -8.0
\begin{array}{l}
\\
-8
\end{array}
Initial program 72.3%
sub-neg72.3%
Applied egg-rr72.3%
sub-neg72.3%
unpow272.3%
unpow272.3%
difference-of-squares72.3%
*-commutative72.3%
+-commutative72.3%
+-commutative72.3%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
Simplified100.0%
Taylor expanded in x around inf 62.8%
Taylor expanded in eps around 0 62.8%
Simplified2.9%
herbie shell --seed 2024152
(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)))