MATLAB: Does the code generation of elementary math functions not respect single-precision floating point data type

abscodedoubleEmbedded Coderfunctionsgenerationmathmatlab codersimulink codersingle

I am using the ABS block inside my Simulink model with input datatype as 'single'. The outport of the model is set to inherit input data type. When I generate code from the model, I see the following:
out_sig_single = (F32)fabs(in_sig_single);
Even though the results would be computationally correct, the generated code uses double precision floating point numbers for operation and then uses cast operator to cast it to a single precision floating point data type. My hardware only supports single precision Math.

Best Answer

This feature of code generation for elementary math functions (such as ABS block) is not supported, as of MATLAB R2019a.
The workaround is to change the standard math library by going to 'Configuration Parameters > All Parameters' and searching for "Standard math library". Then please change this to "C99 (ISO)".
In older releases, you may find this option in 'Configuration Parameters > Code Generation (or Real-Time Workshop) > Interface' and possibly with a different name: "Target function library".
Before changing the library, please make sure to delete the 'slprj' folder present in the current directory.