MATLAB Answers

0

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

Asked by MathWorks Support Team on 27 Jun 2009
Latest activity Commented on by Walter Roberson
on 22 Aug 2019 at 8:41
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.

1 Answer

Answer by MathWorks Support Team on 22 Aug 2019 at 4:00
Edited by MathWorks Support Team on 22 Aug 2019 at 9:02
 Accepted 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.

  2 Comments

Not supported at all, or not supported for single?
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.