Mathematics |
Integer Arithmetic
MATLAB can perform arithmetic operations on arrays of the same integer data type, and the result has the same type. For example,
For a list of the operations that support integer data types, see Nondouble Data Type Support in the arithmetic operators reference page.
When the result of an operation exceeds the maximum value of the data type, MATLAB returns the maximum value. For example,
MATLAB returns 127
, which is the maximum value for numbers of type int8
.
Similarly, if the result is less than the minimum value of the data type, MATLAB returns the minimum value.
You can make MATLAB return a warning when the result of an operation is outside the range an integer data type. Warnings for Integer Data Types explains how to turn these warnings on.
MATLAB computes elementwise division, A./B
and A.\B
, where A
and B
are arrays of integer data type, using elementwise double-precision arithmetic and then converting the result back to the original integer data type. For example,
MATLAB computes 4/3 in double precision and then converts the result to int8
, rounding 4/3 to 1.
Combining Integer Data Types with Scalars of Type double
You can combine scalars or arrays of an integer data type with scalars (but not arrays) of type double
and the result has the same integer data type. For example,
However, you cannot combine scalars or arrays of an integer data type with scalars or arrays of a different integer data type or data type single
.
For all binary operations in which one operand is an array of integer data type and the other is a scalar double
, MATLAB computes the operation using elementwise double-precision arithmetic and then converts the result back to the original integer data type. For example,
MATLAB computes [1 2 3 4 5]*0.8
in double precision and then converts the result to int8
. Note that the second and third entries of [1 2 3 4 5]*0.8
, which are 1.6
and 2.4
, are both rounded to the nearest integer, which is 2
.
Largest and Smallest Values for Integer Data Types | Example -- Digitized Signals |
© 1994-2005 The MathWorks, Inc.