c++ - Determining whether a circular number is nearly equal to another -
i'm working radians within range of [-pi, pi]. values outside range converted valid values following function
double radian::convert(double input) { if (input > rad_upper_bound) return rad_lower_bound + fmod(input, rad_upper_bound); if (input < rad_lower_bound) return rad_upper_bound + fmod(input, rad_upper_bound); return input; }
my question is: how can implement logic check whether radian within x in either direction of radian b. want specifiy x when calling function.
assuming = 3.0; b = -2.5; x = 1; function should return true because , b less x apart.
i'm assuming there standard way of handling type of problem.
the main trick here realize values can't more half total span away each other.
bool iswithindistance(double a,double b,double within_distance) { assert(a >= rad_lower_bound); assert(a <= rad_upper_bound); assert(b >= rad_lower_bound); assert(b <= rad_upper_bound); assert(within_distance >= 0); double span = radian_upper_bound-radian_lower_bound; double distance = fabs(a-b); if (distance > span/2) { distance = span-distance; } return distance<within_distance; }
in example:
a = 3.0; b = -2.5; within_distance = 1; span = 2*pi; distance = fabs(a-b) = 5.5; distance > pi, distance = 2*pi - 5.5 ~= 0.7832; result = (distance < 1) = true;
Comments
Post a Comment