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