-3

For example:

float AngleAddition(float value)
{
   float angle = value + 90;
   return angle;  
 }

If the value passed is 340 to this method it should return a float of value 70 which is in degree. Since 340 + 90 = 360 + 70 (360 is nothing but 0 in degrees).

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
Karthik
  • 101
  • 1
  • 13
  • 3
    http://en.wikipedia.org/wiki/Modular_arithmetic – L.B Aug 13 '14 at 07:09
  • 2
    What is the expected behaviour for *negative* angles? what would the result be if it was -180 + 90 ? – Marc Gravell Aug 13 '14 at 07:22
  • @MarcGravell's point is important, particularly for negative non-integral types where modulo *may* do something unexpected. – Bathsheba Aug 13 '14 at 07:27
  • it would be -90 which is exactly I need ! The only thing is that, it should not be greater than +360 or -360 – Karthik Aug 13 '14 at 07:28
  • 1
    -360 to +360 introduces degeneracy; for example, -90 is the same as +270. Are you sure you need this? – Bathsheba Aug 13 '14 at 07:30
  • @Bathsheba A just question ! For now am passing only positive values, may be it would be a problem in future implementation – Karthik Aug 13 '14 at 07:54

2 Answers2

2

You should use the modulo operator with the 360 value.

public float AngleAddition(float angle, float value)
{
   return (angle + value)%360;  
}

The modulo operator should work for float or double, just keep in mind the limitations of numeric representation. See this question for a case study.

Community
  • 1
  • 1
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
0
private float AngleAddition(float value)
{
   return (value + 90) % 360;  
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189