unity5 - C# - Clamping an angle vs using modulo -
what difference between clamping angle in following code
do { if(angle < -360) { angle += 360; } if(angle > 360) { angle -= 360; } }while(angle < -360 || angle > 360);
... , using modulo arithmetics;
angle = angle % 360;
... , if relevant unity game engine.
assuming angle
integer, difference clamp code allows angles of -360
or +360
degrees. modulo reduce zero.
the comments speak of difference in handling negative values. comments wrong. both approaches leave -10
-10
.
if angle
isn't integer type though, if it's double
instance, %
operator may give different results repeated addition or subtraction, due repeated addition or subtraction introducing rounding problems. in case, %
operator better match. you'd need excessively large angles become problem though, , excessively large angles, don't have enough precision represent angles accurately anyway.
Comments
Post a Comment