English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
MATLAB supports various numeric classes, including signed and unsigned integers as well as single and double precision floating-point numbers. By default, MATLAB stores all numbers as double precision floating-point numbers.
You can choose to store any number or number array as an integer or single precision number.
All numerical types support basic array operations and mathematical operations.
MATLAB provides the following functions to convert to various numerical data types-
function | function |
---|---|
double | convert to double precision number |
single | convert to single precision number |
int8 | convert to8bit signed integer |
int16 | convert to16bit signed integer |
int32 | convert to32bit signed integer |
int64 | convert to64bit signed integer |
uint8 | convert to8bit unsigned integer |
uint16 | convert to16bit unsigned integer |
uint32 | convert to32bit unsigned integer |
uint64 | convert to64bit unsigned integer |
Create a script file and enter the following code-
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
When running the file, it displays the following result-
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
Let's expand the previous example. Create a script file and enter the following code-
x = int32([5.32 3.47 6.28]) .* 7.5 x = int64([5.32 3.47 6.28]) .* 7.5 x = num2cell(x)
When running the file, it displays the following result-
x = 38 23 45 x = 38 23 45 x = { [1,1] = 38 [1,2] = 23 [1,3] = 45 }
functionintmax()andintmin()Return the maximum and minimum values that can be represented by all integer types.
These two functions take integer data types as parameters, for example intmax(int8)orintmin(int64),and return the maximum and minimum values that can be represented by integer data types.
The following examples illustrate how to obtain the minimum and maximum values of integers. Create a script file and write the following code-
%Display the minimum and maximum 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)) %Display the minimum and maximum 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))
When running the file, it displays the following result-
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
functionrealmax()andrealmin()Return the maximum and minimum values that can be represented as floating-point numbers.
When using the parameter 'single' to call the two functions, both functions return the maximum and minimum values that can be represented by single precision data types; when using the parameter 'double' to call, both functions return the maximum and minimum values that can be represented by the parameter, and the double precision data type.
The following example illustrates how to obtain the minimum and maximum floating-point numbers. Create a script file and write the following code in it-
%Display Minimum and Maximum Single Precision %Floating Point Numbers 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')) %Display Minimum and Maximum Double Precision %Floating Point Numbers 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'))
When running the file, it displays the following result-
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