
(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 (+ 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 73.4%
+-commutative73.4%
unpow273.4%
unpow273.4%
difference-of-squares73.4%
sub-neg73.4%
distribute-lft-in73.3%
+-commutative73.3%
distribute-lft-in73.4%
associate-+l+73.4%
remove-double-neg73.4%
sub-neg73.4%
+-commutative73.4%
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 -1.16e-89) (not (<= x 8.5e-91))) (* 2.0 (* eps x)) (* eps eps)))
double code(double x, double eps) {
double tmp;
if ((x <= -1.16e-89) || !(x <= 8.5e-91)) {
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 <= (-1.16d-89)) .or. (.not. (x <= 8.5d-91))) 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 <= -1.16e-89) || !(x <= 8.5e-91)) {
tmp = 2.0 * (eps * x);
} else {
tmp = eps * eps;
}
return tmp;
}
def code(x, eps): tmp = 0 if (x <= -1.16e-89) or not (x <= 8.5e-91): tmp = 2.0 * (eps * x) else: tmp = eps * eps return tmp
function code(x, eps) tmp = 0.0 if ((x <= -1.16e-89) || !(x <= 8.5e-91)) 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 <= -1.16e-89) || ~((x <= 8.5e-91))) tmp = 2.0 * (eps * x); else tmp = eps * eps; end tmp_2 = tmp; end
code[x_, eps_] := If[Or[LessEqual[x, -1.16e-89], N[Not[LessEqual[x, 8.5e-91]], $MachinePrecision]], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision], N[(eps * eps), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.16 \cdot 10^{-89} \lor \neg \left(x \leq 8.5 \cdot 10^{-91}\right):\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\end{array}
\end{array}
if x < -1.15999999999999993e-89 or 8.49999999999999985e-91 < x Initial program 19.3%
+-commutative19.3%
unpow219.3%
unpow219.3%
difference-of-squares19.3%
sub-neg19.3%
distribute-lft-in19.3%
+-commutative19.3%
distribute-lft-in19.3%
associate-+l+19.3%
remove-double-neg19.3%
sub-neg19.3%
+-commutative19.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 0 96.7%
*-commutative96.7%
Simplified96.7%
if -1.15999999999999993e-89 < x < 8.49999999999999985e-91Initial program 95.3%
+-commutative95.3%
unpow295.3%
unpow295.3%
difference-of-squares95.3%
sub-neg95.3%
distribute-lft-in95.3%
+-commutative95.3%
distribute-lft-in95.3%
associate-+l+95.3%
remove-double-neg95.3%
sub-neg95.3%
+-commutative95.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 93.0%
Final simplification94.1%
(FPCore (x eps) :precision binary64 (if (<= x -1.35e-89) (* eps (+ x x)) (if (<= x 8.5e-91) (* eps eps) (* 2.0 (* eps x)))))
double code(double x, double eps) {
double tmp;
if (x <= -1.35e-89) {
tmp = eps * (x + x);
} else if (x <= 8.5e-91) {
tmp = eps * eps;
} else {
tmp = 2.0 * (eps * x);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (x <= (-1.35d-89)) then
tmp = eps * (x + x)
else if (x <= 8.5d-91) then
tmp = eps * eps
else
tmp = 2.0d0 * (eps * x)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (x <= -1.35e-89) {
tmp = eps * (x + x);
} else if (x <= 8.5e-91) {
tmp = eps * eps;
} else {
tmp = 2.0 * (eps * x);
}
return tmp;
}
def code(x, eps): tmp = 0 if x <= -1.35e-89: tmp = eps * (x + x) elif x <= 8.5e-91: tmp = eps * eps else: tmp = 2.0 * (eps * x) return tmp
function code(x, eps) tmp = 0.0 if (x <= -1.35e-89) tmp = Float64(eps * Float64(x + x)); elseif (x <= 8.5e-91) tmp = Float64(eps * eps); else tmp = Float64(2.0 * Float64(eps * x)); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (x <= -1.35e-89) tmp = eps * (x + x); elseif (x <= 8.5e-91) tmp = eps * eps; else tmp = 2.0 * (eps * x); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[x, -1.35e-89], N[(eps * N[(x + x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 8.5e-91], N[(eps * eps), $MachinePrecision], N[(2.0 * N[(eps * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.35 \cdot 10^{-89}:\\
\;\;\;\;\varepsilon \cdot \left(x + x\right)\\
\mathbf{elif}\;x \leq 8.5 \cdot 10^{-91}:\\
\;\;\;\;\varepsilon \cdot \varepsilon\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(\varepsilon \cdot x\right)\\
\end{array}
\end{array}
if x < -1.34999999999999994e-89Initial program 19.8%
sub-neg19.8%
Applied egg-rr19.8%
sub-neg19.8%
unpow219.8%
unpow219.8%
difference-of-squares19.9%
*-commutative19.9%
+-commutative19.9%
+-commutative19.9%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
Simplified100.0%
Taylor expanded in x around inf 97.0%
if -1.34999999999999994e-89 < x < 8.49999999999999985e-91Initial program 95.3%
+-commutative95.3%
unpow295.3%
unpow295.3%
difference-of-squares95.3%
sub-neg95.3%
distribute-lft-in95.3%
+-commutative95.3%
distribute-lft-in95.3%
associate-+l+95.3%
remove-double-neg95.3%
sub-neg95.3%
+-commutative95.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 93.0%
if 8.49999999999999985e-91 < x Initial program 18.8%
+-commutative18.8%
unpow218.8%
unpow218.8%
difference-of-squares18.7%
sub-neg18.7%
distribute-lft-in18.8%
+-commutative18.8%
distribute-lft-in18.7%
associate-+l+18.7%
remove-double-neg18.7%
sub-neg18.7%
+-commutative18.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 0 96.3%
*-commutative96.3%
Simplified96.3%
Final simplification94.1%
(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.4%
+-commutative73.4%
unpow273.4%
unpow273.4%
difference-of-squares73.4%
sub-neg73.4%
distribute-lft-in73.3%
+-commutative73.3%
distribute-lft-in73.4%
associate-+l+73.4%
remove-double-neg73.4%
sub-neg73.4%
+-commutative73.4%
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 71.0%
(FPCore (x eps) :precision binary64 -2.0)
double code(double x, double eps) {
return -2.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = -2.0d0
end function
public static double code(double x, double eps) {
return -2.0;
}
def code(x, eps): return -2.0
function code(x, eps) return -2.0 end
function tmp = code(x, eps) tmp = -2.0; end
code[x_, eps_] := -2.0
\begin{array}{l}
\\
-2
\end{array}
Initial program 73.4%
sub-neg73.4%
Applied egg-rr73.4%
sub-neg73.4%
unpow273.4%
unpow273.4%
difference-of-squares73.4%
*-commutative73.4%
+-commutative73.4%
+-commutative73.4%
associate--l+100.0%
+-inverses100.0%
+-rgt-identity100.0%
Simplified100.0%
Taylor expanded in x around inf 63.3%
add-log-exp36.1%
exp-prod36.1%
flip-+35.9%
div-inv35.9%
+-inverses35.9%
+-inverses35.9%
pow-unpow36.1%
+-inverses36.1%
metadata-eval36.1%
metadata-eval36.1%
+-inverses36.1%
pow-unpow0.0%
+-inverses0.0%
+-inverses0.0%
div-inv0.0%
flip-+36.0%
exp-prod36.0%
*-un-lft-identity36.0%
*-un-lft-identity36.0%
log-prod36.0%
metadata-eval36.0%
Applied egg-rr0.0%
Simplified2.9%
herbie shell --seed 2024188
(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)))