2

See

>> 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

However,

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

ans =

     1.99584030953472e+292

I would have expected the NaN in example 1 to take the answer value from example 2.

Alex
  • 15,186
  • 15
  • 73
  • 127

1 Answers1

3

The issue is that when you create the following array:

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

The first entry is infinity and by definition, eps(Inf) == NaN.

Why does this happen, well realmax('single') returns a single-precision number and realmax('double') returns a double precision number.

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

When you concatenate the two, MATLAB downcasts the entire array to a single precision number which obviously causes the realmax('double')-10 to excede the range of the datatype and it becomes infinity.

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

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

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

When you call eps(realmax('double') - 10) by itself, the very large double is actually a double and eps returns the expected epsilon.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • to be precise I had no idea that `realmin` and `realmax` do not return double values. – Alex May 05 '16 at 03:06
  • 1
    The concatenation conversion table is given [here](http://www.mathworks.com/help/matlab/matlab_prog/valid-combinations-of-unlike-classes.html). And a possible [explanation of "why"](http://www.mathworks.com/matlabcentral/answers/18222-arithmetic-promotion-floating-point-to-integer#answer_24526) appears to be that since non-double types were added later for specific uses, Matlab assumes a non-double array was created for a purpose and should remain that non-double type it was created as, regardless of any operation involving doubles. Which makes sense in a way. @Alex – TroyHaskin May 05 '16 at 05:55
  • @TroyHaskin Thanks for the info! I have been bitten by that so many times particularly when trying to concatenate integers and doubles. I feel like there should (at the least) be a warning when you perform the concatenation. – Suever May 05 '16 at 12:35