MATLAB中eps的使用方法
- MATLAB中eps是一个函数,可以返回某一个数N的最小浮点数精度,形式例如eps(N)。下面我们就通过一些N取不同的值,介绍一下这个函数的详细用法。
eps
Floating-point relative accuracy
Syntax
eps
d = eps(X)
eps('double')
eps('single')
Description
eps returns the distance from 1.0 to the next largest double-precision number, that is eps = 2^(-52).
d = eps(X) is the positive distance from abs(X) to the next larger in magnitude floating point number of the same precision as X. X may be either double precision or single precision. For all X,
eps(X) = eps(-X) = eps(abs(X))
eps('double') is the same as eps or eps(1.0).
eps('single') is the same as eps(single(1.0)) or single(2^-23).
Except for numbers whose absolute value is smaller than realmin , if 2^E <= abs(X) < 2^(E+1), then
eps(X) = 2^(E-23) if isa(X,'single')
eps(X) = 2^(E-52) if isa(X,'double')
For all X of class double such that abs(X) <= realmin, eps(X) = 2^(-1074). Similarly, for all X of class single such that abs(X) <= realmin('single'), eps(X) = 2^(-149).
Replace expressions of the form:
if Y < eps * ABS(X)
with
if Y < eps(X)
- 默认最小浮点数精度
在MATLAB主窗口中输入eps,回车这是MATLAB默认的最小浮点数精度。
精度概念
黑色实心点代表的是数轴的数,例如1,红圈与1的距离代表的就是eps(1),那么在1+eps(1)/2到1之间的所有数,都被认作1,MATLAB无法识别介于1和1+eps(1)/2之间的数。
eps(1),我们看一下下面的例子就明白了
首先是我们看一下eps(1)的值,还是eps的默认值。
然后我们比较1+eps(1)是否与1相等,结果返回的是0;在MATLAB中0即为false,1为true,所以我们知道,1+eps(1)与1并不相等,也就是说,matlab能区分这样的两个数,包括1+0.9*eps(1)以及1+eps(1)0.6,MATLAB都可以区分开,但是当与1的差小于0.5eps(1)的时候,MATLAB已经不能识别,所以,我们可以看到
1
+
e
p
s
(
1
)
∗
0.5
=
=
1
1+eps(1)*0.5==1
1+eps(1)∗0.5==1返回的结果是1,也就是为真,
1
+
e
p
s
(
1
)
∗
0.4
=
=
1
1+eps(1)*0.4==1
1+eps(1)∗0.4==1返回的结果也为真。我们在工程计算的时候尤其要注意这一点,小心小数吃大数。
eps(2)以及eps(N)
我们可以看到最小精度eps(N)随着数量级增大,eps也在逐渐增大,这在计算的过程中,都要注意。