sin.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* sin.c
  2. *
  3. * Circular sine
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, sin();
  10. *
  11. * y = sin( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Range reduction is into intervals of pi/4. The reduction
  18. * error is nearly eliminated by contriving an extended precision
  19. * modular arithmetic.
  20. *
  21. * Two polynomial approximating functions are employed.
  22. * Between 0 and pi/4 the sine is approximated by
  23. * x + x**3 P(x**2).
  24. * Between pi/4 and pi/2 the cosine is represented as
  25. * 1 - x**2 Q(x**2).
  26. *
  27. *
  28. * ACCURACY:
  29. *
  30. * Relative error:
  31. * arithmetic domain # trials peak rms
  32. * DEC 0, 10 150000 3.0e-17 7.8e-18
  33. * IEEE -1.07e9,+1.07e9 130000 2.1e-16 5.4e-17
  34. *
  35. * ERROR MESSAGES:
  36. *
  37. * message condition value returned
  38. * sin total loss x > 1.073741824e9 0.0
  39. *
  40. * Partial loss of accuracy begins to occur at x = 2**30
  41. * = 1.074e9. The loss is not gradual, but jumps suddenly to
  42. * about 1 part in 10e7. Results may be meaningless for
  43. * x > 2**49 = 5.6e14. The routine as implemented flags a
  44. * TLOSS error for x > 2**30 and returns 0.0.
  45. */
  46. /* cos.c
  47. *
  48. * Circular cosine
  49. *
  50. *
  51. *
  52. * SYNOPSIS:
  53. *
  54. * double x, y, cos();
  55. *
  56. * y = cos( x );
  57. *
  58. *
  59. *
  60. * DESCRIPTION:
  61. *
  62. * Range reduction is into intervals of pi/4. The reduction
  63. * error is nearly eliminated by contriving an extended precision
  64. * modular arithmetic.
  65. *
  66. * Two polynomial approximating functions are employed.
  67. * Between 0 and pi/4 the cosine is approximated by
  68. * 1 - x**2 Q(x**2).
  69. * Between pi/4 and pi/2 the sine is represented as
  70. * x + x**3 P(x**2).
  71. *
  72. *
  73. * ACCURACY:
  74. *
  75. * Relative error:
  76. * arithmetic domain # trials peak rms
  77. * IEEE -1.07e9,+1.07e9 130000 2.1e-16 5.4e-17
  78. * DEC 0,+1.07e9 17000 3.0e-17 7.2e-18
  79. */
  80. /* sin.c */
  81. /*
  82. Cephes Math Library Release 2.8: June, 2000
  83. Copyright 1985, 1995, 2000 by Stephen L. Moshier
  84. */
  85. #include "mconf.h"
  86. #ifdef UNK
  87. static double sincof[] = {
  88. 1.58962301576546568060E-10, -2.50507477628578072866E-8,
  89. 2.75573136213857245213E-6, -1.98412698295895385996E-4,
  90. 8.33333333332211858878E-3, -1.66666666666666307295E-1,
  91. };
  92. static double coscof[6] = {
  93. -1.13585365213876817300E-11, 2.08757008419747316778E-9,
  94. -2.75573141792967388112E-7, 2.48015872888517045348E-5,
  95. -1.38888888888730564116E-3, 4.16666666666665929218E-2,
  96. };
  97. static double DP1 = 7.85398125648498535156E-1;
  98. static double DP2 = 3.77489470793079817668E-8;
  99. static double DP3 = 2.69515142907905952645E-15;
  100. /* static double lossth = 1.073741824e9; */
  101. #endif
  102. #ifdef DEC
  103. static unsigned short sincof[] = {
  104. 0030056, 0143750, 0177214, 0163153, 0131727, 0027455, 0044510, 0175352,
  105. 0033470, 0167432, 0131752, 0042414, 0135120, 0006400, 0146776, 0174027,
  106. 0036410, 0104210, 0104207, 0137202, 0137452, 0125252, 0125252, 0125103,
  107. };
  108. static unsigned short coscof[24] = {
  109. 0127107, 0151115, 0002060, 0152325, 0031017, 0072353, 0155161, 0174053,
  110. 0132623, 0171173, 0172542, 0057056, 0034320, 0006400, 0147102, 0023652,
  111. 0135666, 0005540, 0133012, 0076213, 0037052, 0125252, 0125252, 0125126,
  112. };
  113. /* 7.853981629014015197753906250000E-1 */
  114. static unsigned short P1[] = {
  115. 0040111,
  116. 0007732,
  117. 0120000,
  118. 0000000,
  119. };
  120. /* 4.960467869796758577649598009884E-10 */
  121. static unsigned short P2[] = {
  122. 0030410,
  123. 0055060,
  124. 0100000,
  125. 0000000,
  126. };
  127. /* 2.860594363054915898381331279295E-18 */
  128. static unsigned short P3[] = {
  129. 0021523,
  130. 0011431,
  131. 0105056,
  132. 0001560,
  133. };
  134. #define DP1 *(double *)P1
  135. #define DP2 *(double *)P2
  136. #define DP3 *(double *)P3
  137. #endif
  138. #ifdef IBMPC
  139. static unsigned short sincof[] = {
  140. 0x9ccd, 0x1fd1, 0xd8fd, 0x3de5, 0x1f5d, 0xa929, 0xe5e5, 0xbe5a,
  141. 0x48a1, 0x567d, 0x1de3, 0x3ec7, 0xdf03, 0x19bf, 0x01a0, 0xbf2a,
  142. 0xf7d0, 0x1110, 0x1111, 0x3f81, 0x5548, 0x5555, 0x5555, 0xbfc5,
  143. };
  144. static unsigned short coscof[24] = {
  145. 0x1a9b, 0xa086, 0xfa49, 0xbda8, 0x3f05, 0x7b4e, 0xee9d, 0x3e21,
  146. 0x4bc6, 0x7eac, 0x7e4f, 0xbe92, 0x44f5, 0x19c8, 0x01a0, 0x3efa,
  147. 0x4f91, 0x16c1, 0xc16c, 0xbf56, 0x554b, 0x5555, 0x5555, 0x3fa5,
  148. };
  149. /*
  150. 7.85398125648498535156E-1,
  151. 3.77489470793079817668E-8,
  152. 2.69515142907905952645E-15,
  153. */
  154. static unsigned short P1[] = {0x0000, 0x4000, 0x21fb, 0x3fe9};
  155. static unsigned short P2[] = {0x0000, 0x0000, 0x442d, 0x3e64};
  156. static unsigned short P3[] = {0x5170, 0x98cc, 0x4698, 0x3ce8};
  157. #define DP1 *(double *)P1
  158. #define DP2 *(double *)P2
  159. #define DP3 *(double *)P3
  160. #endif
  161. #ifdef MIEEE
  162. static unsigned short sincof[] = {
  163. 0x3de5, 0xd8fd, 0x1fd1, 0x9ccd, 0xbe5a, 0xe5e5, 0xa929, 0x1f5d,
  164. 0x3ec7, 0x1de3, 0x567d, 0x48a1, 0xbf2a, 0x01a0, 0x19bf, 0xdf03,
  165. 0x3f81, 0x1111, 0x1110, 0xf7d0, 0xbfc5, 0x5555, 0x5555, 0x5548,
  166. };
  167. static unsigned short coscof[24] = {
  168. 0xbda8, 0xfa49, 0xa086, 0x1a9b, 0x3e21, 0xee9d, 0x7b4e, 0x3f05,
  169. 0xbe92, 0x7e4f, 0x7eac, 0x4bc6, 0x3efa, 0x01a0, 0x19c8, 0x44f5,
  170. 0xbf56, 0xc16c, 0x16c1, 0x4f91, 0x3fa5, 0x5555, 0x5555, 0x554b,
  171. };
  172. static unsigned short P1[] = {0x3fe9, 0x21fb, 0x4000, 0x0000};
  173. static unsigned short P2[] = {0x3e64, 0x442d, 0x0000, 0x0000};
  174. static unsigned short P3[] = {0x3ce8, 0x4698, 0x98cc, 0x5170};
  175. #define DP1 *(double *)P1
  176. #define DP2 *(double *)P2
  177. #define DP3 *(double *)P3
  178. #endif
  179. #ifdef ANSIPROT
  180. extern double polevl(double, void *, int);
  181. extern double p1evl(double, void *, int);
  182. extern double floor(double);
  183. extern double ldexp(double, int);
  184. extern int isnan(double);
  185. extern int isfinite(double);
  186. #else
  187. double polevl(), floor(), ldexp();
  188. int isnan(), isfinite();
  189. #endif
  190. extern double PIO4;
  191. static double lossth = 1.073741824e9;
  192. #ifdef NANS
  193. extern double NAN;
  194. #endif
  195. #ifdef INFINITIES
  196. extern double INFINITY;
  197. #endif
  198. double sin(x) double x;
  199. {
  200. double y, z, zz;
  201. int j, sign;
  202. #ifdef MINUSZERO
  203. if (x == 0.0)
  204. return (x);
  205. #endif
  206. #ifdef NANS
  207. if (isnan(x))
  208. return (x);
  209. if (!isfinite(x)) {
  210. mtherr("sin", DOMAIN);
  211. return (NAN);
  212. }
  213. #endif
  214. /* make argument positive but save the sign */
  215. sign = 1;
  216. if (x < 0) {
  217. x = -x;
  218. sign = -1;
  219. }
  220. if (x > lossth) {
  221. mtherr("sin", TLOSS);
  222. return (0.0);
  223. }
  224. y = floor(x / PIO4); /* integer part of x/PIO4 */
  225. /* strip high bits of integer part to prevent integer overflow */
  226. z = ldexp(y, -4);
  227. z = floor(z); /* integer part of y/8 */
  228. z = y - ldexp(z, 4); /* y - 16 * (y/16) */
  229. j = z; /* convert to integer for tests on the phase angle */
  230. /* map zeros to origin */
  231. if (j & 1) {
  232. j += 1;
  233. y += 1.0;
  234. }
  235. j = j & 07; /* octant modulo 360 degrees */
  236. /* reflect in x axis */
  237. if (j > 3) {
  238. sign = -sign;
  239. j -= 4;
  240. }
  241. /* Extended precision modular arithmetic */
  242. z = ((x - y * DP1) - y * DP2) - y * DP3;
  243. zz = z * z;
  244. if ((j == 1) || (j == 2)) {
  245. y = 1.0 - ldexp(zz, -1) + zz * zz * polevl(zz, coscof, 5);
  246. } else {
  247. /* y = z + z * (zz * polevl( zz, sincof, 5 ));*/
  248. y = z + z * z * z * polevl(zz, sincof, 5);
  249. }
  250. if (sign < 0)
  251. y = -y;
  252. return (y);
  253. }
  254. double cos(x) double x;
  255. {
  256. double y, z, zz;
  257. long i;
  258. int j, sign;
  259. #ifdef NANS
  260. if (isnan(x))
  261. return (x);
  262. if (!isfinite(x)) {
  263. mtherr("cos", DOMAIN);
  264. return (NAN);
  265. }
  266. #endif
  267. /* make argument positive */
  268. sign = 1;
  269. if (x < 0)
  270. x = -x;
  271. if (x > lossth) {
  272. mtherr("cos", TLOSS);
  273. return (0.0);
  274. }
  275. y = floor(x / PIO4);
  276. z = ldexp(y, -4);
  277. z = floor(z); /* integer part of y/8 */
  278. z = y - ldexp(z, 4); /* y - 16 * (y/16) */
  279. /* integer and fractional part modulo one octant */
  280. i = z;
  281. if (i & 1) /* map zeros to origin */
  282. {
  283. i += 1;
  284. y += 1.0;
  285. }
  286. j = i & 07;
  287. if (j > 3) {
  288. j -= 4;
  289. sign = -sign;
  290. }
  291. if (j > 1)
  292. sign = -sign;
  293. /* Extended precision modular arithmetic */
  294. z = ((x - y * DP1) - y * DP2) - y * DP3;
  295. zz = z * z;
  296. if ((j == 1) || (j == 2)) {
  297. /* y = z + z * (zz * polevl( zz, sincof, 5 ));*/
  298. y = z + z * z * z * polevl(zz, sincof, 5);
  299. } else {
  300. y = 1.0 - ldexp(zz, -1) + zz * zz * polevl(zz, coscof, 5);
  301. }
  302. if (sign < 0)
  303. y = -y;
  304. return (y);
  305. }
  306. /* Degrees, minutes, seconds to radians: */
  307. /* 1 arc second, in radians = 4.8481368110953599358991410e-5 */
  308. #ifdef DEC
  309. static unsigned short P648[] = {
  310. 034513,
  311. 054170,
  312. 0176773,
  313. 0116043,
  314. };
  315. #define P64800 *(double *)P648
  316. #else
  317. static double P64800 = 4.8481368110953599358991410e-5;
  318. #endif
  319. double radian(d, m, s) double d, m, s;
  320. { return (((d * 60.0 + m) * 60.0 + s) * P64800); }