
(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 eps eps (* x (* eps 2.0))))
double code(double x, double eps) {
return fma(eps, eps, (x * (eps * 2.0)));
}
function code(x, eps) return fma(eps, eps, Float64(x * Float64(eps * 2.0))) end
code[x_, eps_] := N[(eps * eps + N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, \varepsilon, x \cdot \left(\varepsilon \cdot 2\right)\right)
\end{array}
Initial program 70.7%
+-commutative70.7%
unpow270.7%
unpow270.7%
difference-of-squares70.7%
sub-neg70.7%
distribute-lft-in70.7%
+-commutative70.7%
distribute-lft-in70.7%
associate-+l+70.7%
remove-double-neg70.7%
sub-neg70.7%
+-commutative70.7%
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%
Final simplification100.0%
(FPCore (x eps) :precision binary64 (if (or (<= x -2e-113) (not (<= x 3.1e-135))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -2e-113) || !(x <= 3.1e-135)) {
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 <= (-2d-113)) .or. (.not. (x <= 3.1d-135))) 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 <= -2e-113) || !(x <= 3.1e-135)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -2e-113) or not (x <= 3.1e-135): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -2e-113) || !(x <= 3.1e-135)) 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 <= -2e-113) || ~((x <= 3.1e-135))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -2e-113], N[Not[LessEqual[x, 3.1e-135]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-113} \lor \neg \left(x \leq 3.1 \cdot 10^{-135}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -1.99999999999999996e-113 or 3.1000000000000001e-135 < x Initial program 33.2%
+-commutative33.2%
unpow233.2%
unpow233.2%
difference-of-squares33.2%
sub-neg33.2%
distribute-lft-in33.2%
+-commutative33.2%
distribute-lft-in33.2%
associate-+l+33.2%
remove-double-neg33.2%
sub-neg33.2%
+-commutative33.2%
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 91.1%
*-commutative91.1%
Simplified91.1%
if -1.99999999999999996e-113 < x < 3.1000000000000001e-135Initial program 98.9%
+-commutative98.9%
unpow298.9%
unpow298.9%
difference-of-squares99.0%
sub-neg99.0%
distribute-lft-in98.9%
+-commutative98.9%
distribute-lft-in99.0%
associate-+l+98.9%
remove-double-neg98.9%
sub-neg98.9%
+-commutative98.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 97.1%
Final simplification94.5%
(FPCore (x eps) :precision binary64 (if (<= x -1.05e-114) (* 2.0 (* eps x)) (if (<= x 3.1e-135) (* eps eps) (* x (* eps 2.0)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.05e-114) {
tmp = 2.0 * (eps * x);
} else if (x <= 3.1e-135) {
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.05d-114)) then
tmp = 2.0d0 * (eps * x)
else if (x <= 3.1d-135) 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.05e-114) {
tmp = 2.0 * (eps * x);
} else if (x <= 3.1e-135) {
tmp = eps * eps;
} else {
tmp = x * (eps * 2.0);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.05e-114: tmp = 2.0 * (eps * x) elif x <= 3.1e-135: tmp = eps * eps else: tmp = x * (eps * 2.0) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.05e-114) tmp = Float64(2.0 * Float64(eps * x)); elseif (x <= 3.1e-135) 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.05e-114) tmp = 2.0 * (eps * x); elseif (x <= 3.1e-135) tmp = eps * eps; else tmp = x * (eps * 2.0); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.05e-114], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 3.1e-135], N[(eps * eps), $MachinePrecision], N[(x * N[(eps * 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.05 \cdot 10^{-114}:\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{elif}\;x \leq 3.1 \cdot 10^{-135}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(\varepsilon \cdot 2\right)\\
\end{array}
\end{array}
if x < -1.04999999999999996e-114Initial program 31.2%
+-commutative31.2%
unpow231.2%
unpow231.2%
difference-of-squares31.1%
sub-neg31.1%
distribute-lft-in31.1%
+-commutative31.1%
distribute-lft-in31.1%
associate-+l+31.1%
remove-double-neg31.1%
sub-neg31.1%
+-commutative31.1%
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 89.4%
*-commutative89.4%
Simplified89.4%
if -1.04999999999999996e-114 < x < 3.1000000000000001e-135Initial program 98.9%
+-commutative98.9%
unpow298.9%
unpow298.9%
difference-of-squares99.0%
sub-neg99.0%
distribute-lft-in98.9%
+-commutative98.9%
distribute-lft-in99.0%
associate-+l+98.9%
remove-double-neg98.9%
sub-neg98.9%
+-commutative98.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 97.1%
if 3.1000000000000001e-135 < x Initial program 35.2%
+-commutative35.2%
unpow235.2%
unpow235.2%
difference-of-squares35.1%
sub-neg35.1%
distribute-lft-in35.2%
+-commutative35.2%
distribute-lft-in35.1%
associate-+l+35.1%
remove-double-neg35.1%
sub-neg35.1%
+-commutative35.1%
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 100.0%
*-commutative100.0%
unpow2100.0%
associate-/l*99.9%
distribute-lft-out99.9%
Simplified99.9%
Taylor expanded in eps around 0 92.9%
Final simplification94.6%
(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 70.7%
+-commutative70.7%
unpow270.7%
unpow270.7%
difference-of-squares70.7%
sub-neg70.7%
distribute-lft-in70.7%
+-commutative70.7%
distribute-lft-in70.7%
associate-+l+70.7%
remove-double-neg70.7%
sub-neg70.7%
+-commutative70.7%
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 (* 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 70.7%
+-commutative70.7%
unpow270.7%
unpow270.7%
difference-of-squares70.7%
sub-neg70.7%
distribute-lft-in70.7%
+-commutative70.7%
distribute-lft-in70.7%
associate-+l+70.7%
remove-double-neg70.7%
sub-neg70.7%
+-commutative70.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 inf 69.3%
herbie shell --seed 2024185
(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)))