modulo - behind the scenes



  • While cleaning up some code, both in R19 and R20, I noticed I sometimes use a % b to calculate a modulo, and sometimes LModulo(a, b) (where a and b are both Int32 variables or values).
    Obviously, the LModulo is defined in the Cinema 4D SDK and thus preferred. But what is the benefit of using it over the regular modulus operator, or put differently, what are the cons of using the modulus operator?

    Just wondering.



  • Hi Daniel, thanks for reaching out us.

    With regard to the LModulo function, being it part of the cinema.framework, you can see, by looking at the implementation, that it:

    • it's the specialized version for integers with either Int32 or Int64 parameters' data-type;
    • handles properly the case of the first argument being negative;
    • handles gracefully the case where the second argument is zero.

    The code, available in the framework is:

    if (!b)
    	return 0;
    if (a >= 0)
    	return a % b;
    
    a -= (a / b) * b;
    if (a < 0)
    	a += b;
    
    return a;
    

    Cheers, Riccardo



  • Thanks for the info.


Log in to reply