Reputation: 69
The code I'm reading has a line:
someData = nan(3, 4, 5, "single")
I can't find the documentation for the "nan" function, but this code appears to generate a 3x4x5 matrix of NaN. However, I don't know what "single" does. Replacing it with a random other string gets "error: NaN: invalid data type specified", and replacing it with "double" appears to give the same result. What is the function of the "single"?
Upvotes: 1
Views: 208
Reputation: 65430
It makes the resulting matrix of nan
a matrix that is of the single-precision data type which contains single-precision floating-point numbers. If you want single precision, you need to specify this explicitly, otherwise Octave and MATLAB will use double-precision by default.
You can check the class of the output using class
.
class(nan(3, 4, 5, 'single'))
% 'single'
class(nan(3, 4, 5))
% 'double'
As far as looking the same, they will look the same until you start trying to store numbers that exceed the range of numbers that can be represented with single-precision floating point representation. This is because single precision numbers use half the amount of memory that double precision numbers do.
a = nan(1, 1, 'single');
a(1) = 1e-64
% 0
b = nan(1, 1);
b(1) = 1e-64
% 1.000e-64
Also if we inspect the variables with whos
we can confirm the size difference.
a = nan(1,1,'single');
b = nan(1,1)
whos('a', 'b')
% Variables in the current scope:
%
% Attr Name Size Bytes Class
% ==== ==== ==== ===== =====
% a 1x1 4 single
% b 1x1 8 double
Upvotes: 4