sin.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. C program for floating point sin/cos.
  3. Calls modf.
  4. There are no error exits.
  5. Coefficients are #3370 from Hart & Cheney (18.80D).
  6. */
  7. #include <u.h>
  8. #include <libc.h>
  9. #define p0 .1357884097877375669092680e8
  10. #define p1 -.4942908100902844161158627e7
  11. #define p2 .4401030535375266501944918e6
  12. #define p3 -.1384727249982452873054457e5
  13. #define p4 .1459688406665768722226959e3
  14. #define q0 .8644558652922534429915149e7
  15. #define q1 .4081792252343299749395779e6
  16. #define q2 .9463096101538208180571257e4
  17. #define q3 .1326534908786136358911494e3
  18. static
  19. double
  20. sinus(double arg, int quad)
  21. {
  22. double e, f, ysq, x, y, temp1, temp2;
  23. int k;
  24. x = arg;
  25. if(x < 0) {
  26. x = -x;
  27. quad += 2;
  28. }
  29. x *= 1/PIO2; /* underflow? */
  30. if(x > 32764) {
  31. y = modf(x, &e);
  32. e += quad;
  33. modf(0.25*e, &f);
  34. quad = e - 4*f;
  35. } else {
  36. k = x;
  37. y = x - k;
  38. quad += k;
  39. quad &= 3;
  40. }
  41. if(quad & 1)
  42. y = 1-y;
  43. if(quad > 1)
  44. y = -y;
  45. ysq = y*y;
  46. temp1 = ((((p4*ysq+p3)*ysq+p2)*ysq+p1)*ysq+p0)*y;
  47. temp2 = ((((ysq+q3)*ysq+q2)*ysq+q1)*ysq+q0);
  48. return temp1/temp2;
  49. }
  50. double
  51. cos(double arg)
  52. {
  53. if(arg < 0)
  54. arg = -arg;
  55. return sinus(arg, 1);
  56. }
  57. double
  58. sin(double arg)
  59. {
  60. return sinus(arg, 0);
  61. }