Main Content

Combining Integers and Double-Precision Numbers

MATLAB® supports the combination of integers of the same class and scalar double-precision numbers. MATLAB does not support the combination of integers and single-precision numbers. If you use the MATLAB Coder™ app or codegen with the -singleC option to generate single-precision C/C++ code, your MATLAB code cannot combine integers and double-precision numbers. Converting an expression that combines integers and doubles results in an illegal MATLAB expression. To work around this limitation, cast the numbers so that the types of the numbers match. Either cast the integer numbers to double-precision or cast the double-precision numbers to the integer class.

For example, consider the function dut that returns the sum of a and b.

function c = dut(a,b)
c = a + b;

Generate single-precision code using codegen with the -singleC option. Specify that the first argument is double and the second argument is int32.

 codegen -singleC -config:lib dut -args {0, int32(2)} -report

Code generation fails. The message suggests that you cast the operands so that they have the same types.

Rewrite the code so that it cast a to the type of b.

function c = dut(a,b)
c = int32(a) + b;