2

I have a variable which is a Modulus of Congruence x=y(mod 360),which means y varies from 0 - 360 and if the value is greater than 360 it again comes to 0. For example x=5 for y = 365 .

I wrote this function to stabilize y , so if the difference between x and previousx is greater than 5 then i get x otherwise previousx .

float stabilize(float x,float previous){

    if(fabs(x-previousx)<5)
    {
        return previousx;
    }
    else
    {
        return x;
    }

}

This works fine between 0 to 360 But this fails on the boundary condition of 360 and 0 .How can i stabilize the value when y is a value near 0 such as 0.3 and previous y is near 360 such as 359. So the difference calculated here is 359 - .3 = 358.7 . but i want it to be the modulo 360 difference which is 1.3 .

rajat
  • 3,415
  • 15
  • 56
  • 90
  • So you want the absolute value of the angle difference between `x` and `y`? – Mysticial Aug 03 '12 at 09:55
  • no , i am already taking the absolute value using fabs . – rajat Aug 03 '12 at 10:04
  • Okay, now I'm very confused about what the question is. So you already have a difference, but you want to normalize it to something like `[0,360)`? Can you give us examples of your inputs and expected outputs? – Mysticial Aug 03 '12 at 10:06
  • ok , the input is always mod 360 . For example the inputs to the function, x = 358 and previousx = 2 , i want the output to be previousx and if x = 356 and previousx = 3 i want the output to be 356 . – rajat Aug 03 '12 at 10:09
  • The variables names might be creating the confusion , i have edited question to make the question more clearer . – rajat Aug 03 '12 at 10:13
  • 2
    Oh okay, you're just trying to test if the angle between `x` and `previousx` is less than `5`. [My answer here](http://stackoverflow.com/a/11498248/922184) has an `angleDiff()` function that will give you the difference between any two angles. The result is normalized to `[-180, 180)`. So that might do what you want. – Mysticial Aug 03 '12 at 10:22
  • Okay , will try it out . – rajat Aug 03 '12 at 11:35

2 Answers2

4

What about something like if(fabs(x-previousx)<5 || fabs(x-previousx)>355)? Given that the input data is mod 360, if the difference is big enough it means that both values are close enough to the border.

ekholm
  • 2,543
  • 18
  • 18
1

You could subtract the original numbers and take modulo 360 of the fabs(result):

for example:

359 - 720.3 = -361.3
fabs(-361.3) = 361.3
361.3 % 360 = 1.3
user1157159
  • 23
  • 1
  • 6
  • I have no way of getting the original numbers , i am using a external library for getting the angles . – rajat Aug 03 '12 at 10:01