Reputation: 13
What is the difference between clamping an angle like in the following code
do
{
if(angle < -360)
{
angle += 360;
}
if(angle > 360)
{
angle -= 360;
}
}while(angle < -360 || angle > 360);
... and using the modulo arithmetics;
angle = angle % 360;
... and if this is relevant for the Unity game engine.
Upvotes: 1
Views: 1160
Reputation:
Assuming angle
is an integer, the difference is that your clamp code allows angles of exactly -360
or +360
degrees. Modulo would reduce that to zero.
The comments speak of a difference in handling negative values. Those comments are wrong. Both approaches would leave -10
as -10
.
If angle
isn't an integer type though, if it's double
for instance, the %
operator may give different results than repeated addition or subtraction, due to repeated addition or subtraction introducing rounding problems. In that case, the %
operator would be a better match. You'd need excessively large angles for this to become a problem though, and with those excessively large angles, you don't have enough precision to represent angles accurately anyway.
Upvotes: 2