Advertisement
Ember

sincos

Mar 29th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. Float2 SinCos(Float angle)
  2. {
  3.     // Map Value to y in [-pi,pi], x = 2*pi*quotient + remainder.
  4.     Float quotient = InvPiX2 * angle;
  5.  
  6.     if(angle >= 0.0f) { quotient = (float)((int)(quotient + 0.5f)); } else { quotient = (float)((int)(quotient - 0.5f)); }
  7.     Float y = angle - PiX2 * quotient;
  8.  
  9.     // Map y to [-pi/2,pi/2] with sin(y) = sin(Value).
  10.     Float sign;
  11.     if(y > PiDiv2)
  12.     {
  13.         y = Pi - y;
  14.         sign = -1.0f;
  15.     }
  16.     else if(y < -PiDiv2)
  17.     {
  18.         y = -Pi - y;
  19.         sign = -1.0f;
  20.     }
  21.     else
  22.     {
  23.         sign = +1.0f;
  24.     }
  25.  
  26.     float y2 = y * y;
  27.  
  28.     // Sin gets an 11-degree minimax approximation
  29.     // Cosine gets a 10-degree minimax approximation
  30.     Float2 result;
  31.     result.x = (((((-2.3889859e-08f * y2 + 2.7525562e-06f) * y2 - 0.00019840874f) * y2 + 0.0083333310f) * y2 - 0.16666667f) * y2 + 1.0f) * y;
  32.     result.y = (((((-2.6051615e-07f * y2 + 2.4760495e-05f) * y2 - 0.0013888378f) * y2 + 0.041666638f) * y2 - 0.5f) * y2 + 1.0f) * sign;
  33.     return result;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement