MATLAB Answers

Why does the code generation of elementary math functions not respect single-precision floating point data type?

1 view (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Aug 2019
Edited: MathWorks Support Team on 22 Aug 2019
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.
Walter Roberson
Walter Roberson on 22 Aug 2019
The C standard library does not define any single precision floating point absolute value until C99, which introduced fabsf() and cabsf()
C99 defines that on systems that support IEEE 754 that double (a mandatory C datatype) is to be double precision. However it does permit non-754 systems to define double the same as single, in which case it does not matter that the code reads double because datatype double would be single precision.
Therefore the distinction between single and double is only relevant for non-compliant systems that know the difference between single and double but do not implement double.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by