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.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()`函式返回可以用所有型別的整數表示的最大值和最小值。

``````% 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()`函式返回可以用浮點數表示的最大值和最小值。

``````% 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'
``````

以下是糾正/補充內容：

int_maxint8 應為 intmax'int8' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  提交時間：2019-08-13