# MATLAB数字

MATLAB 支持的数字类有很多，其中包括符号和无符号的整数及单精度和双精度浮点数。

MATLAB可以选择存储任何数字或数字为整数或单精度数字阵列。

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.9000   26.0250   47.1000
x =

39.9000   26.0250   47.1000
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 =
[38]    [23]    [45]``````

## 最小和最大整数

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

## 详细例子

``````% displaying the smallest and largest signed integer data
str = 'The range for int8 is:
%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))
str = 'The range for int16 is:
%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))
str = 'The range for int32 is:
%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))
str = 'The range for int64 is:
%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))

% displaying the smallest and largest unsigned integer data
str = 'The range for uint8 is:
%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))
str = 'The range for uint16 is:
%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))
str = 'The range for uint32 is:
%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))
str = 'The range for uint64 is:
%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
``````

## 详细实例

``````% displaying the smallest and largest single-precision
% floating w3cschool number
str = 'The range for single is:
%g to %g and
%g to  %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
realmin('single'), realmax('single'))
% displaying the smallest and largest double-precision
% floating w3cschool number
str = 'The range for double is:
%g to %g and
%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
``````