I am attempting to convert a negative angle (in degrees) to positive. But I am getting a compile error saying:
test.cpp invalid operands of types 'double' and 'int' to binary 'operator%'
test.cpp invalid operands of types 'float' and 'int' to binary 'operator%'
My code:
double to_positive_angle(double angle)
{
return ((3600000 + angle) % 360);
}
float to_positive_angle(float angle)
{
return ((3600000 + angle) % 360);
}
Its obviously because I am trying to use the Modulus operator on a Float and Double.
Are the any ways I can successfully convert a negative angle (float) to a positive one (float)? Or ways that I can overcome the modulus compile error?
Given a negative angle, we add 360 to get its corresponding positive angle.
Negative Degrees to Radian The method to convert a negative degree into a radian is the same as we have done for positive degrees. Multiply the given value of the angle in degrees by π/180.
For a negative angle, you simply need to rotate in the other direction (i.e., clockwise instead of anti-clockwise). This means that for an angle of −40∘, you end up at the same point (and with the same values of sin and cos as an angle of 320∘.
a positive angle starts from an initial side and moves clockwise to its terminal side. A negative angle starts from an initial side and moves counterclockwise to its terminal side.
This version works for all possible inputs, not just ones greater than 3600000, and solves the %
issue you were experiencing.
double to_positive_angle(double angle)
{
angle = fmod(angle, 360);
if (angle < 0) angle += 360;
return angle;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With