Advertisement
Kitomas

fast sin, cos, tan approximations without using crt

Jun 7th, 2024
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #ifndef M_PI
  2. #define M_PI (3.1415926535897932384626433)
  3. #endif
  4. #define M_PI2 (M_PI/2)
  5. #define M_2PI (2*M_PI)
  6. #define _fmod(_x, _y) (   (_x) - (  (long long)((_x)/(_y)) * (_y)  )   )
  7. //(you could switch floats with doubles here, though the approximation's
  8.  //lack of precision prevents that switch from being all that useful)
  9. float sinf_bhaskara(float x){
  10.     //keep x within the domain of >=0  ->  <pi,
  11.      //while preserving relevant info
  12.     char negative = x<0.0f;
  13.     if(x < 0.0f) x = -x; //x = fabsf(x);
  14.     x = _fmod(x, M_2PI); //x %= 2pi
  15.     //'if original value of x%(2pi) is between _ -> _, returned value will be _':
  16.      //>-2pi -> <=-pi,  >=0
  17.      //> -pi -> <=  0,  <=0
  18.      //>=  0 -> <  pi,  >=0
  19.      //>= pi -> < 2pi,  <=0
  20.     negative ^= x>=M_PI;
  21.     if(x >= M_PI) x -= M_PI; //x %= pi
  22.    
  23.     float result = 16*x * (M_PI-x);
  24.     result /= 5*M_PI*M_PI - 4*x*(M_PI-x);
  25.     return (negative) ? -result : result;
  26.    
  27. }
  28.  
  29. #define sinf sinf_bhaskara
  30. #define cosf(_x) sinf((_x)+M_PI2)
  31. #define tanf(_x) ( sinf(_x)/cosf(_x) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement