简体   繁体   English

参数“单”的八度nan函数

[英]Octave nan function with argument “single”

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. 我找不到“ nan”功能的文档,但是此代码似乎生成了一个3x4x5的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. 用其他随机字符串替换它会得到“错误:NaN:指定了无效的数据类型”,而用“ double”替换似乎会得到相同的结果。 What is the function of the "single"? “单身”的功能是什么?

It makes the resulting matrix of nan a matrix that is of the single-precision data type which contains single-precision floating-point numbers. 它使所得的nan矩阵成为具有精度浮点数的单精度数据类型的矩阵。 If you want single precision, you need to specify this explicitly, otherwise Octave and MATLAB will use double-precision by default. 如果需要单精度,则需要显式指定,否则默认情况下Octave和MATLAB将使用双精度。

You can check the class of the output using class . 您可以使用class检查输出的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. 同样,如果我们与whos一起检查变量,我们可以确认大小差异。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM