C++, fastest method of getting angle into a specified range? -
What is the fastest way to obtain a variable in the given range? For example, make sure an angle "double alpha" is always inside (0.0, 2 * Pi).
I get two solutions, one of them is very slow and the other seems to be complicated so easy should a better way be a better way or not?
// is concise but very slow (hence it's none); Return Asin (sin (alpha)); // very fast, but looks ugly (two while loops to change a variable! Come!) While (alpha = 0.0) {alpha + = 2.0 * M_PI; } While (alpha> = 2.0 * M_PI) {alpha - = 2.0 * M_PI; } Return alpha; Instead of applying it by hand, I suggest that you fmod
Function family: which actually does this you still have to take into account the negative consequences, though: alpha = FMOD (alpha, 2.0 * mopi); If (alpha & lt; 0.0) alpha + = 2.0 * M_PI; Return alpha;
Comments
Post a Comment