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

39 visualizaciones (últimos 30 días)
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);

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 14 de En. de 2026 a las 0:00
Editada: MathWorks Support Team el 14 de En. de 2026 a las 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 comentarios
Walter Roberson
Walter Roberson el 22 de Ag. de 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 el 6 de Sept. de 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()

Iniciar sesión para comentar.

Más respuestas (1)

Andy Bartlett
Andy Bartlett el 15 de En. de 2026 a las 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.

Productos


Versión

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by