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

조회 수: 39 (최근 30일)
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);

채택된 답변

MathWorks Support Team
MathWorks Support Team 2026년 1월 29일 0:00
편집: MathWorks Support Team 대략 7시간 전
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 https://www.mathworks.com/help/releases/R2025b/rtw/ref/languagestandard.html 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 https://www.mathworks.com/help/releases/R2021a/rtw/ref/standard-math-library.html for additional information.
Another way to get remove all double-precision floating-point usage in your Simulink model and its generated code is to use the https://www.mathworks.com/help/fixedpoint/ug/getting-started-with-the-double-to-single-converter.html.
This tool not only ensures that code generation is configured for C99, but also updates your model so that signals, parameters, default data type propagation, and other relevant settings use 'single' instead of 'double'. This helps ensure that both your model and the generated code operate entirely with single-precision, which is especially useful for hardware that does not support double-precision math.
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 8월 22일
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 2023년 9월 6일
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()

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andy Bartlett
Andy Bartlett 2026년 1월 15일 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!

Translated by