math.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. }
  134. static const int N = 100;
  135. double coef(int n)
  136. {
  137. double t;
  138. if (n == 0)
  139. {
  140. return 0;
  141. }
  142. t = 1.0/n;
  143. if (n%2 == 0)
  144. {
  145. t = -t;
  146. }
  147. return t;
  148. }
  149. double horner(double x)
  150. {
  151. double u = coef(N);
  152. int i;
  153. for(i=N-1; i>=0; i--)
  154. {
  155. u = u*x + coef(i);
  156. }
  157. return u;
  158. }
  159. double sqrt(double b)
  160. {
  161. double x = 1;
  162. int step = 0;
  163. while ((x*x-b<-0.000000000000001 || x*x-b>0.000000000000001) && step<50)
  164. {
  165. x = (b/x+x)/2.0;
  166. step++;
  167. }
  168. return x;
  169. }
  170. double ln(double x)
  171. {
  172. int i;
  173. if (x > 1.5)
  174. {
  175. for(i=0; x>1.25; i++)
  176. {
  177. x = sqrt(x);
  178. }
  179. return (1<<i)*horner(x-1);
  180. }
  181. else if (x<0.7 && x>0)
  182. {
  183. for(i=0; x<0.7; i++)
  184. {
  185. x = sqrt(x);
  186. }
  187. return (1<<i)*horner(x-1);
  188. }
  189. else if(x > 0)
  190. {
  191. return horner(x-1);
  192. }
  193. }
  194. double exp(double x)
  195. {
  196. double sum = 1;
  197. int i;
  198. for(i=N; i>0; i--)
  199. {
  200. sum /= i;
  201. sum *= x;
  202. sum += 1;
  203. }
  204. return sum;
  205. }
  206. double pow(double m, double n)
  207. {
  208. return exp(n*ln(m));
  209. }