MATLAB支持包罗有标记和无标记整数以及单精度和双精度浮点数的各类数字范例。 默认环境下,MATLAB将所有数值存储为双精度浮点数。
可以选择将任何数字或数组的数字存储为整数或单精度数字。
所有数字范例都支持根基的数组运算和数学运算。
转换为各类数值数据范例
MATLAB提供以下函数来将数值转换为各类数字数据范例 –
函数 | 描写说明 |
---|---|
double |
转换为双精度数 |
single |
转换为单精度数 |
int8 |
转换为8 位有标记整数 |
int16 |
转换为16 位有标记整数 |
int32 |
转换为32 位有标记整数 |
int64 |
转换为64 位有标记整数 |
uint8 |
转换为8 位无标记整数 |
uint16 |
转换为16 位无标记整数 |
uint32 |
转换为32 位无标记整数 |
uint64 |
转换为64 位无标记整数 |
示例
建设剧本文件并键入以下代码 –
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)
执行上面示例代码,获得以下功效 –
Trial>> x = int32([5.32 3.47 6.28]) .* 7.5
x = int64([5.32 3.47 6.28]) .* 7.5
x = num2cell(x)
x =
1×3 int32 行向量
38 23 45
x =
1×3 int64 行向量
38 23 45
x =
1×3 cell 数组
{[38]} {[23]} {[45]}
最小和最大的整数
intmax()
和intmin()
函数返回可以用所有范例的整数暗示的最大值和最小值。
这两个函数将整数数据范例作为参数,譬喻int_max(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:
-9223372036854775808 to 9223372036854775807 '
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 4294967295 '
ans =
'The range for uint64 is:
0 to 1.844674e+19 '
最小和最大的浮点数
realmax()
和realmin()
函数返回可以用浮点数暗示的最大值和最小值。
#p#分页标题#e#
当利用参数'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'