From -PI...PI wrapper

Hello all :slight_smile:

So I’ve been looking into faster sin/cos functions lately and found code for a faster method than Math… that I’ve come to adopt in my code. Thing is, bit of code requires that the input value be between -PI…PI. Their suggestion was an if/then that would add/ subtract 2*PI… Which meant that that would have to be in a while loop, too, (while value > PI…) until it is within range. Considering that I’m trying to optimize/speed up calls to sin/cos, that’d kinda defeat the purpose. So I took it upon myself to figure it out in one blow:


// xx = input value in radians
var Sign:Number = ((xx < 0) ? -1 : 1); // if input value is +/-...
/* xx is brought within realms of -PI..PI:
   -Pi * 
         (1 if xx/Pi is odd, ie wraps back towards -PI ; 0 if even, ie tends to +PI )
   + xx mod pi (remainder, will be +/- depending on orig value of xx); */

// So we have...
xx = (-Sign*PI * (int(xx / PI) & 1)) + ( xx % PI); 


Now. I’d like to know if that is an efficient/effective/practical method, or if there’s an easier way to get that value than what I’ve devised, as I’m hardly either mathematically or computer logically savvy lol, and I figure that I’ve gone a bit overkill and there’s actually a simpler way to do this. Any help/hints/tips would be appreciated!