Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistency with Math.Round()

I have two functions that are intended to contain angles between (-180,180] and (-π,π]. The intent is that given any angle from -inf to +inf it will retain the equivalent angle in the intervals specified. For example the angle for 1550° is 110°.

public double WrapBetween180(double angle)
{
    return angle - 360d * Math.Round(angle / 360d, MidpointRounding.AwayFromZero);
}
public double WrapBetweenPI(double angle)
{
    const double twopi = 2d * Math.PI;
    return angle - twopi * Math.Round(angle / twopi, MidpointRounding.AwayFromZero);
}

which yields the following results

WrapBetween180(-180) = -180
WrapBetween180( 180) =  180

WrapBetweenPI(-Math.PI) =  Math.PI
WrapBetweenPI( Math.PI) = -Math.PI

none of which is what I want. What I wanted is:

WrapBetween180(-180) =  180
WrapBetween180( 180) =  180

WrapBetweenPI(-Math.PI) =  Math.PI
WrapBetweenPI( Math.PI) =  Math.PI

I tryied playing around with the rounding methods, but still cannot get the desired results. The problem is pronounced because sometimes the angles I deal with are only approximately close to -π or π and I am getting discontinuities it my results.

Any suggestions on how to best implement angle wrapping functions with non-inclusive low limit and inclusive high limits?

like image 262
John Alexiou Avatar asked Jan 22 '26 18:01

John Alexiou


1 Answers

For the angle in degrees, if x is between -180 and 180, then 180 - x is between 0 and 360. What you want is equivalent to asking that 180 - x is between 0 (inclusive), and 360 (exclusive). So, as soon as 180 - x reaches 360, we want to add 360 to the angle. This gives us:

return angle + 360d * Math.Floor((180d - angle) / 360d);

Same thing for the angle in radians:

return angle + twopi * Math.Floor((Math.PI - angle) / twopi);
like image 183
Jeffrey Sax Avatar answered Jan 24 '26 10:01

Jeffrey Sax