Modulo Operator in Embedded Coder

조회 수: 7 (최근 30일)
Lukas Wild
Lukas Wild 2021년 6월 9일
답변: Prahlad Gowtham Katte 2022년 2월 16일
Hello !
I am using the mod block in Simulink (for modulo operation) from the math tools.
I want then to generate code with embedded coder, which is working but why is the code generation creating a custom modulo function like:
real_T rt_modd_snf(real_T u0, real_T u1)
{
real_T y;
boolean_T yEq;
real_T q;
y = u0;
if (u1 == 0.0) {
if (u0 == 0.0) {
y = u1;
}
} else if (rtIsNaN(u0) || rtIsNaN(u1) || rtIsInf(u0)) {
y = (rtNaN);
} else if (u0 == 0.0) {
y = 0.0 / u1;
} else if (rtIsInf(u1)) {
if ((u1 < 0.0) != (u0 < 0.0)) {
y = u1;
}
} else {
y = fmod(u0, u1);
yEq = (y == 0.0);
if ((!yEq) && (u1 > floor(u1))) {
q = fabs(u0 / u1);
yEq = !(fabs(q - floor(q + 0.5)) > DBL_EPSILON * q);
}
if (yEq) {
y = u1 * 0.0;
} else {
if ((u0 < 0.0) != (u1 < 0.0)) {
y += u1;
}
}
}
return y;
}
and not using the standard C modulo function ? I just want to have: output = _angle % 360;
How can I achieve this output ?
Thank you

답변 (1개)

Prahlad Gowtham Katte
Prahlad Gowtham Katte 2022년 2월 16일
Hello,
I understand that you want to know the reason behind the embedded coder’s own function definition instead of the % (modulo)operator usage in generated C/C++ code.
The reason is that when Embedded Coder generates a code for modulo block it also tries to cover some invalid cases such as (0%0 )etc. While as if we use a direct % operator compiler will give an error. Hence after verifying that the inputs are valid then fmod function is used which is the function for performing modulus operation in C.
Hope it helps

카테고리

Help CenterFile Exchange에서 Embedded Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by