MATLAB - Numbers
MATLAB - Numeric Types
Section titled “MATLAB - Numeric Types”MATLAB is designed for numerical computation and supports a rich set of numeric data types. These include various sizes of signed and unsigned integers, as well as single-precision and double-precision floating-point numbers. By default, MATLAB stores all numeric values as double, which is a 64-bit double-precision floating-point type.
While double is suitable for most scientific and engineering calculations, using other types can be crucial for specific tasks, such as saving memory, performing integer arithmetic, or interfacing with hardware that expects specific data formats.
Numeric Data Type Conversion
Section titled “Numeric Data Type Conversion”MATLAB provides functions to explicitly convert a variable to a specific numeric type. When converting from a floating-point number to an integer, MATLAB rounds the value to the nearest integer.
| Function | Purpose |
|---|---|
| double | Converts to 64-bit double-precision floating-point |
| single | Converts to 32-bit single-precision floating-point |
| 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 |
Example: Type Conversion and its Effects
Section titled “Example: Type Conversion and its Effects”Create a script to see how conversion affects a calculation. Note how floating-point results are rounded when converted to integers.
% Start with a default double-precision arrayoriginal_data = [2.71, -3.14, 8.99];
% Perform a calculation, the result is also a doubleresult_double = original_data * 10.5class(result_double)
% Convert to single precision (less memory, less precision)result_single = single(result_double)class(result_single)
% Convert to a 16-bit integer (values are rounded)result_int16 = int16(result_double)class(result_int16)When you run the file, the output will be:
result_double = 28.4550 -32.9700 94.3950ans = 'double'
result_single = single
28.4550 -32.9700 94.3950ans = 'single'
result_int16 = int16
28 -33 94ans = 'int16'Numeric Type Ranges
Section titled “Numeric Type Ranges”Each numeric type can only represent a finite range of values. The functions intmin, intmax, realmin, and realmax are used to find these limits.
Integer Ranges
Section titled “Integer Ranges”The following script demonstrates how to display the range for various integer types.
% Displaying the ranges for signed integersfprintf('Range for int8: %d to %d\n', intmin('int8'), intmax('int8'));fprintf('Range for int16: %d to %d\n', intmin('int16'), intmax('int16'));fprintf('Range for int32: %d to %d\n', intmin('int32'), intmax('int32'));
% For 64-bit integers, use num2str as %d format specifier may not support themmin_i64 = num2str(intmin('int64'));max_i64 = num2str(intmax('int64'));fprintf('Range for int64: %s to %s\n', min_i64, max_i64);
% Displaying the ranges for unsigned integersfprintf('\nRange for uint8: %u to %u\n', intmin('uint8'), intmax('uint8'));fprintf('Range for uint16: %u to %u\n', intmin('uint16'), intmax('uint16'));fprintf('Range for uint32: %u to %u\n', intmin('uint32'), intmax('uint32'));
min_u64 = num2str(intmin('uint64'));max_u64 = num2str(intmax('uint64'));fprintf('Range for uint64: %s to %s\n', min_u64, max_u64);When you run the file, it produces the correct ranges:
Range for int8: -128 to 127Range for int16: -32768 to 32767Range for int32: -2147483648 to 2147483647Range for int64: -9223372036854775808 to 9223372036854775807
Range for uint8: 0 to 255Range for uint16: 0 to 65535Range for uint32: 0 to 4294967295Range for uint64: 0 to 18446744073709551615Floating-Point Ranges
Section titled “Floating-Point Ranges”Similarly, realmin and realmax show the smallest positive and largest finite floating-point numbers.
% Displaying the range for single-precision floatsfprintf('Single precision positive range: %g to %g\n', ... realmin('single'), realmax('single'));
% Displaying the range for double-precision floatsfprintf('Double precision positive range: %g to %g\n', ... realmin('double'), realmax('double'));This will display the following result:
Single precision positive range: 1.17549e-38 to 3.40282e+38Double precision positive range: 2.22507e-308 to 1.79769e+308Best Practices
Section titled “Best Practices”- Stick to
double: Use the defaultdoubletype for all general-purpose calculations unless you have a compelling reason to change. - Use Integers for Integer Data: If you are working with data that is inherently integer-based (like counts, indices, or pixel values from 0-255), use the appropriate integer type (e.g.,
uint8for images). This saves memory and can prevent subtle floating-point errors. - Beware of Overflow: When performing arithmetic with integers, be aware that results that exceed the type’s range will ‘saturate’ at the minimum or maximum value. For example,
int8(120) + int8(10)results in127. singlefor Memory: Usesingleprecision for very large arrays where memory is a concern and the reduced precision is acceptable.