# Modulo Operator in Embedded Coder

9 views (last 30 days)
Lukas Wild on 9 Jun 2021
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

Prahlad Gowtham Katte on 16 Feb 2022
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