Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert negative angle to positive: Involves invalid operand use

Tags:

c++

c

math

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?

like image 734
sazr Avatar asked May 17 '13 01:05

sazr


People also ask

How do you convert a negative angle to a positive angle?

Given a negative angle, we add 360 to get its corresponding positive angle.

How do you convert negative angles to radians?

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.

What if my angle is negative?

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∘.

How does a negative angle form?

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.


1 Answers

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;
}
like image 152
R.. GitHub STOP HELPING ICE Avatar answered Sep 29 '22 04:09

R.. GitHub STOP HELPING ICE