math.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <math.h>
  2. /*
  3. * COPYRIGHT: See COPYING in the top level directory
  4. * PROJECT: ReactOS CRT
  5. * FILE: lib/crt/math/cos.c
  6. * PURPOSE: Generic C Implementation of cos
  7. * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
  8. */
  9. #define PRECISION 9
  10. static double cos_off_tbl[] = {0.0, -M_PI/2., 0, -M_PI/2.};
  11. static double cos_sign_tbl[] = {1,-1,-1,1};
  12. static double sin_off_tbl[] = {0.0, -M_PI/2., 0, -M_PI/2.};
  13. static double sin_sign_tbl[] = {1,-1,-1,1};
  14. double sin(double x)
  15. {
  16. int quadrant;
  17. double x2, result;
  18. /* Calculate the quadrant */
  19. quadrant = x * (2./M_PI);
  20. /* Get offset inside quadrant */
  21. x = x - quadrant * (M_PI/2.);
  22. /* Normalize quadrant to [0..3] */
  23. quadrant = (quadrant - 1) & 0x3;
  24. /* Fixup value for the generic function */
  25. x += sin_off_tbl[quadrant];
  26. /* Calculate the negative of the square of x */
  27. x2 = - (x * x);
  28. /* This is an unrolled taylor series using <PRECISION> iterations
  29. * Example with 4 iterations:
  30. * result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
  31. * To save multiplications and to keep the precision high, it's performed
  32. * like this:
  33. * result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!))))
  34. */
  35. /* Start with 0, compiler will optimize this away */
  36. result = 0;
  37. #if (PRECISION >= 10)
  38. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
  39. result *= x2;
  40. #endif
  41. #if (PRECISION >= 9)
  42. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
  43. result *= x2;
  44. #endif
  45. #if (PRECISION >= 8)
  46. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
  47. result *= x2;
  48. #endif
  49. #if (PRECISION >= 7)
  50. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14);
  51. result *= x2;
  52. #endif
  53. #if (PRECISION >= 6)
  54. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12);
  55. result *= x2;
  56. #endif
  57. #if (PRECISION >= 5)
  58. result += 1./(1.*2*3*4*5*6*7*8*9*10);
  59. result *= x2;
  60. #endif
  61. result += 1./(1.*2*3*4*5*6*7*8);
  62. result *= x2;
  63. result += 1./(1.*2*3*4*5*6);
  64. result *= x2;
  65. result += 1./(1.*2*3*4);
  66. result *= x2;
  67. result += 1./(1.*2);
  68. result *= x2;
  69. result += 1;
  70. /* Apply correct sign */
  71. result *= sin_sign_tbl[quadrant];
  72. return result;
  73. }
  74. double cos(double x)
  75. {
  76. int quadrant;
  77. double x2, result;
  78. /* Calculate the quadrant */
  79. quadrant = x * (2./M_PI);
  80. /* Get offset inside quadrant */
  81. x = x - quadrant * (M_PI/2.);
  82. /* Normalize quadrant to [0..3] */
  83. quadrant = quadrant & 0x3;
  84. /* Fixup value for the generic function */
  85. x += cos_off_tbl[quadrant];
  86. /* Calculate the negative of the square of x */
  87. x2 = - (x * x);
  88. /* This is an unrolled taylor series using <PRECISION> iterations
  89. * Example with 4 iterations:
  90. * result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
  91. * To save multiplications and to keep the precision high, it's performed
  92. * like this:
  93. * result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!))))
  94. */
  95. /* Start with 0, compiler will optimize this away */
  96. result = 0;
  97. #if (PRECISION >= 10)
  98. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
  99. result *= x2;
  100. #endif
  101. #if (PRECISION >= 9)
  102. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
  103. result *= x2;
  104. #endif
  105. #if (PRECISION >= 8)
  106. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
  107. result *= x2;
  108. #endif
  109. #if (PRECISION >= 7)
  110. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14);
  111. result *= x2;
  112. #endif
  113. #if (PRECISION >= 6)
  114. result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12);
  115. result *= x2;
  116. #endif
  117. #if (PRECISION >= 5)
  118. result += 1./(1.*2*3*4*5*6*7*8*9*10);
  119. result *= x2;
  120. #endif
  121. result += 1./(1.*2*3*4*5*6*7*8);
  122. result *= x2;
  123. result += 1./(1.*2*3*4*5*6);
  124. result *= x2;
  125. result += 1./(1.*2*3*4);
  126. result *= x2;
  127. result += 1./(1.*2);
  128. result *= x2;
  129. result += 1;
  130. /* Apply correct sign */
  131. result *= cos_sign_tbl[quadrant];
  132. return result;
  133. }