📜  MATLAB-数字

📅  最后修改于: 2020-11-03 09:50:14             🧑  作者: Mango


MATLAB支持各种数字类,包括有符号和无符号整数以及单精度和双精度浮点数。默认情况下,MATLAB将所有数值存储为双精度浮点数。

您可以选择将任何数字或数字数组存储为整数或单精度数字。

所有数值类型都支持基本数组运算和数学运算。

转换为各种数值数据类型

MATLAB提供以下功能以转换为各种数值数据类型-

Function Purpose
double Converts to double precision number
single Converts to single precision number
int8 Converts to 8-bit signed integer
int16 Converts to 16-bit signed integer
int32 Converts to 32-bit signed integer
int64 Converts to 64-bit signed integer
uint8 Converts to 8-bit unsigned integer
uint16 Converts to 16-bit unsigned integer
uint32 Converts to 32-bit unsigned integer
uint64 Converts to 64-bit unsigned integer

创建一个脚本文件并输入以下代码-

x = single([5.32 3.47 6.28]) .* 7.5
x = double([5.32 3.47 6.28]) .* 7.5
x = int8([5.32 3.47 6.28]) .* 7.5
x = int16([5.32 3.47 6.28]) .* 7.5
x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5

运行文件时,它显示以下结果-

x =

   39.900   26.025   47.100

x =

   39.900   26.025   47.100

x =

   38  23  45

x =

   38  23  45

x =

   38  23  45

x =

   38  23  45

让我们再扩展一下前面的示例。创建一个脚本文件并输入以下代码-

x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5
x = num2cell(x)

运行文件时,它显示以下结果-

x =

   38  23  45

x =

   38  23  45

x = 
{
   [1,1] = 38
   [1,2] = 23
   [1,3] = 45
}

最小和最大整数

函数intmax()intmin()返回可以用所有类型的整数表示的最大值和最小值。

这两个函数都将整数数据类型作为参数,例如intmax(int8)或intmin(int64),并返回可以用整数数据类型表示的最大值和最小值。

以下示例说明了如何获取整数的最小和最大值。创建一个脚本文件并在其中写入以下代码-

% displaying the smallest and largest signed integer data
str = 'The range for int8 is:\n\t%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))

str = 'The range for int16 is:\n\t%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))

str = 'The range for int32 is:\n\t%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))

str = 'The range for int64 is:\n\t%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))
 
% displaying the smallest and largest unsigned integer data
str = 'The range for uint8 is:\n\t%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))

str = 'The range for uint16 is:\n\t%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))

str = 'The range for uint32 is:\n\t%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))

str = 'The range for uint64 is:\n\t%d to %d ';
sprintf(str, intmin('uint64'), intmax('uint64'))

运行文件时,它显示以下结果-

ans = The range for int8 is:
    -128 to 127 
ans = The range for int16 is:
    -32768 to 32767 
ans = The range for int32 is:
    -2147483648 to 2147483647 
ans = The range for int64 is:
    0 to 0 
ans = The range for uint8 is:
    0 to 255 
ans = The range for uint16 is:
    0 to 65535 
ans = The range for uint32 is:
    0 to -1 
ans = The range for uint64 is:
    0 to 18446744073709551616 

最小和最大浮点数

函数realmax()realmin()返回可以用浮点数表示的最大值和最小值。

当使用参数“ single”调用两个函数时,两个函数都返回您可以用单精度数据类型表示的最大值和最小值;当使用参数“ double”调用时,这两个函数都返回您可以用参数表示的最大值和最小值双精度数据类型。

下面的示例说明如何获取最小和最大的浮点数。创建一个脚本文件并在其中写入以下代码-

% displaying the smallest and largest single-precision 
% floating point number
str = 'The range for single is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
   realmin('single'), realmax('single'))

% displaying the smallest and largest double-precision 
% floating point number
str = 'The range for double is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax('double'), -realmin('double'), ...
   realmin('double'), realmax('double'))

运行文件时,它显示以下结果-

ans = The range for single is:                                                  
        -3.40282e+38 to -1.17549e-38 and                                        
         1.17549e-38 to  3.40282e+38                                            
ans = The range for double is:                                                  
        -1.79769e+308 to -2.22507e-308 and                                      
         2.22507e-308 to  1.79769e+308