为什么 eps 在与 realmax 一起使用时在矩阵中失败

Why does eps fail in a matrix when used with realmax

>> eps([1 2 0.00001; (realmax('double')-10) realmin('double') realmax('single')])

ans =

    1.192093e-07    2.384186e-07    9.094947e-13
             NaN    1.401298e-45    2.028241e+31

然而,

>> eps(realmax('double') - 10)

ans =

     1.99584030953472e+292

我本以为示例 1 中的 NaN 会采用示例 2 中的答案值。

问题是当您创建以下数组时:

[(realmax('double')-10) realmin('double') realmax('single')]
%//    Inf    0    3.402823e+38

第一项是无穷大,根据定义,eps(Inf) == NaN

为什么会这样,嗯realmax('single')returns一个单精度数,realmax('double')returns一个双精度数。

REALMAX('single') returns the largest finite floating point number in IEEE single precision.

当您将两者连接起来时,MATLAB downcasts the entire array 成为一个 single 精度数,这显然会导致 realmax('double')-10 超出数据类型的范围并变为无穷大。

class(realmax('double'))
%// double

class(realmax('single'))
%// single

class([(realmax('double')-10) realmin('double') realmax('single')])
%// single

当您单独调用 eps(realmax('double') - 10) 时,非常大的 double 实际上是一个 doubleeps returns 预期的 epsilon。