Modulo Operator in Embedded Coder

15 ビュー (過去 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 Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by