sin.c 1.2 KB

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