Next: Predicates for Numeric Objects, Previous: Logical Values, Up: Numeric Data Types [Contents][Index]

Many operators and functions can work with mixed data types. For example,

uint8 (1) + 1 ⇒ 2

single (1) + 1 ⇒ 2

min (single (1), 0) ⇒ 0

where the results are respectively of types uint8, single, and single respectively. This is done for MATLAB compatibility. Valid mixed operations are defined as follows:

Mixed Operation | Result | ||
---|---|---|---|

double OP single | single | ||

double OP integer | integer | ||

double OP char | double | ||

double OP logical | double | ||

single OP integer | integer | ||

single OP char | single | ||

single OP logical | single |

When functions expect a double but are passed other types, automatic conversion is function-dependent:

a = det (int8 ([1 2; 3 4])) ⇒ a = -2 class (a) ⇒ double

a = eig (int8 ([1 2; 3 4])) ⇒ error: eig: wrong type argument 'int8 matrix'

When two operands are both integers but of different widths, then some cases convert them to the wider bitwidth, and other cases throw an error:

a = min (int8 (100), int16 (200)) ⇒ 100 class (a) ⇒ int16

int8 (100) + int16 (200) ⇒ error: binary operator '+' not implemented for 'int8 scalar' by 'int16 scalar' operations

For two integer operands, they typically need to both be signed or both be unsigned. Mixing signed and unsigned usually causes an error, even if they are of the same bitwidth.

min (int16 (100), uint16 (200)) ⇒ error: min: cannot compute min (int16 scalar, uint16 scalar)

In the case of mixed type indexed assignments, the type is not changed. For example,

x = ones (2, 2); x(1, 1) = single (2) ⇒ x = 2 1 1 1

where `x`

remains of the double precision type.