Skip to content

MATLAB - Numbers

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.

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.

FunctionPurpose
doubleConverts to 64-bit double-precision floating-point
singleConverts to 32-bit single-precision floating-point
int8Converts to 8-bit signed integer
int16Converts to 16-bit signed integer
int32Converts to 32-bit signed integer
int64Converts to 64-bit signed integer
uint8Converts to 8-bit unsigned integer
uint16Converts to 16-bit unsigned integer
uint32Converts to 32-bit unsigned integer
uint64Converts to 64-bit unsigned integer

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 array
original_data = [2.71, -3.14, 8.99];
% Perform a calculation, the result is also a double
result_double = original_data * 10.5
class(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.3950
ans =
'double'
result_single =
single
28.4550 -32.9700 94.3950
ans =
'single'
result_int16 =
int16
28 -33 94
ans =
'int16'

Each numeric type can only represent a finite range of values. The functions intmin, intmax, realmin, and realmax are used to find these limits.

The following script demonstrates how to display the range for various integer types.

% Displaying the ranges for signed integers
fprintf('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 them
min_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 integers
fprintf('\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 127
Range for int16: -32768 to 32767
Range for int32: -2147483648 to 2147483647
Range for int64: -9223372036854775808 to 9223372036854775807
Range for uint8: 0 to 255
Range for uint16: 0 to 65535
Range for uint32: 0 to 4294967295
Range for uint64: 0 to 18446744073709551615

Similarly, realmin and realmax show the smallest positive and largest finite floating-point numbers.

% Displaying the range for single-precision floats
fprintf('Single precision positive range: %g to %g\n', ...
realmin('single'), realmax('single'));
% Displaying the range for double-precision floats
fprintf('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+38
Double precision positive range: 2.22507e-308 to 1.79769e+308
  • Stick to double: Use the default double type 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., uint8 for 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 in 127.
  • single for Memory: Use single precision for very large arrays where memory is a concern and the reduced precision is acceptable.