I am trying to wrap the angle to be within the range of [-pi, pi]. I have tried many variations of the following code, it should be a very easy task, however, if the angle is already pi, -pi, or 0, it outputs it as negative (so pi = -pi, -pi = pi, 0 = -0).
Here is the code I currently have:
static double wrapAngle( double angle){
while (angle < -M_PI) {
angle += 2.0 * M_PI;
}
while (angle > M_PI) {
angle -= 2.0 * M_PI;
}
return angle;
}
It correctly wraps everything except -M_PI, M_PI, and 0. It gives me the negative.
New contributor
leftoverear is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.