Why does generated code use the double version of math functions when the data are single precision floats?

39 views (last 30 days)
I am using Simulink with Embedded Coder. When I generate code from my model, the generated code contains output like the following:
out1 = (real32_T)sin(in1);
out2 = (real32_T)fabs(in2);
out3 = (real32_T)floor(in3);
out4 = (real32_T)log10(in4);
In each case, the generated code is using the version of the math function that takes and returns a double even though all of the "in" and "out" variables have a float data type. 
Even though the results of this code would be computationally correct, this seems like a wasteful way to perform the calculation. Additionally, some of my hardware only supports single precision math. Is there a way to make this operation only use single precision types? For example, use the version of the math functions that take and return a "float" like below?
out1 = sinf(in1);
out2 = fabsf(in2);
out3 = floorf(in3);
out4 = log10f(in4);

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Jan 2026 at 0:00
Edited: MathWorks Support Team on 14 Jan 2026 at 20:14
The reason for these extra data type conversions is likely due to the version of the C language standard you are generating code for. In C89/90, the standard library does not provide versions of the math functions that operate on floats. There are only the versions that take and return doubles. The floating point versions of these functions were introduced in the C99 language standard. The issue can be resolved by changing the setting that controls which version of the C language standard you are generating code for.
In MATLAB R2021b and newer, the setting for adjusting the C language standard is called "Language Standard" and is available under "Configuration Parameters > Code Generation." Adjusting this setting to "C99 (ISO)" should resolve the issue. Please refer to the "Language Standard" doc page for additional information.
In MATLAB R2021a and older, the setting was called "Standard Math Library" and is available under "Configuration Parameters > Code Generation > Interface > Advanced Parameters." Again, changing its value to "C99 (ISO)" should resolve the issue. Please refer to the "Standard Math Library" doc page for additional information.
  3 Comments
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.
Martin Becker
Martin Becker on 6 Sep 2023
In more recent versions of MATLAB/Simulink, the option is in the model settings (Ctrl+E) under Code Generation > Language standard > C99 (ISO). Then fabs() becomes fabsf()

Sign in to comment.

More Answers (1)

Andy Bartlett
Andy Bartlett on 15 Jan 2026 at 18:44
The easiest way to get rid of all double precision floating-point in your Simulink model and its generated code is to use Single Precision Converter App.
This will make sure code generation is configured for C99 as described in the other answer.
It will also change the settings for signals, parameters, default data type propagation choice, etc. to use single instead of double.

Community Treasure Hunt

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

Start Hunting!