sin.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <math.h>
  86. #ifdef UNK
  87. static double sincof[] = {
  88. 1.58962301576546568060E-10,
  89. -2.50507477628578072866E-8,
  90. 2.75573136213857245213E-6,
  91. -1.98412698295895385996E-4,
  92. 8.33333333332211858878E-3,
  93. -1.66666666666666307295E-1,
  94. };
  95. static double coscof[6] = {
  96. -1.13585365213876817300E-11,
  97. 2.08757008419747316778E-9,
  98. -2.75573141792967388112E-7,
  99. 2.48015872888517045348E-5,
  100. -1.38888888888730564116E-3,
  101. 4.16666666666665929218E-2,
  102. };
  103. static double DP1 = 7.85398125648498535156E-1;
  104. static double DP2 = 3.77489470793079817668E-8;
  105. static double DP3 = 2.69515142907905952645E-15;
  106. /* static double lossth = 1.073741824e9; */
  107. #endif
  108. #ifdef DEC
  109. static unsigned short sincof[] = {
  110. 0030056,0143750,0177214,0163153,
  111. 0131727,0027455,0044510,0175352,
  112. 0033470,0167432,0131752,0042414,
  113. 0135120,0006400,0146776,0174027,
  114. 0036410,0104210,0104207,0137202,
  115. 0137452,0125252,0125252,0125103,
  116. };
  117. static unsigned short coscof[24] = {
  118. 0127107,0151115,0002060,0152325,
  119. 0031017,0072353,0155161,0174053,
  120. 0132623,0171173,0172542,0057056,
  121. 0034320,0006400,0147102,0023652,
  122. 0135666,0005540,0133012,0076213,
  123. 0037052,0125252,0125252,0125126,
  124. };
  125. /* 7.853981629014015197753906250000E-1 */
  126. static unsigned short P1[] = {0040111,0007732,0120000,0000000,};
  127. /* 4.960467869796758577649598009884E-10 */
  128. static unsigned short P2[] = {0030410,0055060,0100000,0000000,};
  129. /* 2.860594363054915898381331279295E-18 */
  130. static unsigned short P3[] = {0021523,0011431,0105056,0001560,};
  131. #define DP1 *(double *)P1
  132. #define DP2 *(double *)P2
  133. #define DP3 *(double *)P3
  134. #endif
  135. #ifdef IBMPC
  136. static unsigned short sincof[] = {
  137. 0x9ccd,0x1fd1,0xd8fd,0x3de5,
  138. 0x1f5d,0xa929,0xe5e5,0xbe5a,
  139. 0x48a1,0x567d,0x1de3,0x3ec7,
  140. 0xdf03,0x19bf,0x01a0,0xbf2a,
  141. 0xf7d0,0x1110,0x1111,0x3f81,
  142. 0x5548,0x5555,0x5555,0xbfc5,
  143. };
  144. static unsigned short coscof[24] = {
  145. 0x1a9b,0xa086,0xfa49,0xbda8,
  146. 0x3f05,0x7b4e,0xee9d,0x3e21,
  147. 0x4bc6,0x7eac,0x7e4f,0xbe92,
  148. 0x44f5,0x19c8,0x01a0,0x3efa,
  149. 0x4f91,0x16c1,0xc16c,0xbf56,
  150. 0x554b,0x5555,0x5555,0x3fa5,
  151. };
  152. /*
  153. 7.85398125648498535156E-1,
  154. 3.77489470793079817668E-8,
  155. 2.69515142907905952645E-15,
  156. */
  157. static unsigned short P1[] = {0x0000,0x4000,0x21fb,0x3fe9};
  158. static unsigned short P2[] = {0x0000,0x0000,0x442d,0x3e64};
  159. static unsigned short P3[] = {0x5170,0x98cc,0x4698,0x3ce8};
  160. #define DP1 *(double *)P1
  161. #define DP2 *(double *)P2
  162. #define DP3 *(double *)P3
  163. #endif
  164. #ifdef MIEEE
  165. static unsigned short sincof[] = {
  166. 0x3de5,0xd8fd,0x1fd1,0x9ccd,
  167. 0xbe5a,0xe5e5,0xa929,0x1f5d,
  168. 0x3ec7,0x1de3,0x567d,0x48a1,
  169. 0xbf2a,0x01a0,0x19bf,0xdf03,
  170. 0x3f81,0x1111,0x1110,0xf7d0,
  171. 0xbfc5,0x5555,0x5555,0x5548,
  172. };
  173. static unsigned short coscof[24] = {
  174. 0xbda8,0xfa49,0xa086,0x1a9b,
  175. 0x3e21,0xee9d,0x7b4e,0x3f05,
  176. 0xbe92,0x7e4f,0x7eac,0x4bc6,
  177. 0x3efa,0x01a0,0x19c8,0x44f5,
  178. 0xbf56,0xc16c,0x16c1,0x4f91,
  179. 0x3fa5,0x5555,0x5555,0x554b,
  180. };
  181. static unsigned short P1[] = {0x3fe9,0x21fb,0x4000,0x0000};
  182. static unsigned short P2[] = {0x3e64,0x442d,0x0000,0x0000};
  183. static unsigned short P3[] = {0x3ce8,0x4698,0x98cc,0x5170};
  184. #define DP1 *(double *)P1
  185. #define DP2 *(double *)P2
  186. #define DP3 *(double *)P3
  187. #endif
  188. #ifdef ANSIPROT
  189. extern double polevl ( double, void *, int );
  190. extern double p1evl ( double, void *, int );
  191. extern double floor ( double );
  192. extern double ldexp ( double, int );
  193. extern int isnan ( double );
  194. extern int isfinite ( double );
  195. #else
  196. double polevl(), floor(), ldexp();
  197. int isnan(), isfinite();
  198. #endif
  199. extern double PIO4;
  200. static double lossth = 1.073741824e9;
  201. #ifdef NANS
  202. extern double NAN;
  203. #endif
  204. #ifdef INFINITIES
  205. extern double INFINITY;
  206. #endif
  207. double sin(x)
  208. double x;
  209. {
  210. double y, z, zz;
  211. int j, sign;
  212. #ifdef MINUSZERO
  213. if( x == 0.0 )
  214. return(x);
  215. #endif
  216. #ifdef NANS
  217. if( isnan(x) )
  218. return(x);
  219. if( !isfinite(x) )
  220. {
  221. mtherr( "sin", DOMAIN );
  222. return(NAN);
  223. }
  224. #endif
  225. /* make argument positive but save the sign */
  226. sign = 1;
  227. if( x < 0 )
  228. {
  229. x = -x;
  230. sign = -1;
  231. }
  232. if( x > lossth )
  233. {
  234. mtherr( "sin", TLOSS );
  235. return(0.0);
  236. }
  237. y = floor( x/PIO4 ); /* integer part of x/PIO4 */
  238. /* strip high bits of integer part to prevent integer overflow */
  239. z = ldexp( y, -4 );
  240. z = floor(z); /* integer part of y/8 */
  241. z = y - ldexp( z, 4 ); /* y - 16 * (y/16) */
  242. j = z; /* convert to integer for tests on the phase angle */
  243. /* map zeros to origin */
  244. if( j & 1 )
  245. {
  246. j += 1;
  247. y += 1.0;
  248. }
  249. j = j & 07; /* octant modulo 360 degrees */
  250. /* reflect in x axis */
  251. if( j > 3)
  252. {
  253. sign = -sign;
  254. j -= 4;
  255. }
  256. /* Extended precision modular arithmetic */
  257. z = ((x - y * DP1) - y * DP2) - y * DP3;
  258. zz = z * z;
  259. if( (j==1) || (j==2) )
  260. {
  261. y = 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  262. }
  263. else
  264. {
  265. /* y = z + z * (zz * polevl( zz, sincof, 5 ));*/
  266. y = z + z * z * z * polevl( zz, sincof, 5 );
  267. }
  268. if(sign < 0)
  269. y = -y;
  270. return(y);
  271. }
  272. double cos(x)
  273. double x;
  274. {
  275. double y, z, zz;
  276. long i;
  277. int j, sign;
  278. #ifdef NANS
  279. if( isnan(x) )
  280. return(x);
  281. if( !isfinite(x) )
  282. {
  283. mtherr( "cos", DOMAIN );
  284. return(NAN);
  285. }
  286. #endif
  287. /* make argument positive */
  288. sign = 1;
  289. if( x < 0 )
  290. x = -x;
  291. if( x > lossth )
  292. {
  293. mtherr( "cos", TLOSS );
  294. return(0.0);
  295. }
  296. y = floor( x/PIO4 );
  297. z = ldexp( y, -4 );
  298. z = floor(z); /* integer part of y/8 */
  299. z = y - ldexp( z, 4 ); /* y - 16 * (y/16) */
  300. /* integer and fractional part modulo one octant */
  301. i = z;
  302. if( i & 1 ) /* map zeros to origin */
  303. {
  304. i += 1;
  305. y += 1.0;
  306. }
  307. j = i & 07;
  308. if( j > 3)
  309. {
  310. j -=4;
  311. sign = -sign;
  312. }
  313. if( j > 1 )
  314. sign = -sign;
  315. /* Extended precision modular arithmetic */
  316. z = ((x - y * DP1) - y * DP2) - y * DP3;
  317. zz = z * z;
  318. if( (j==1) || (j==2) )
  319. {
  320. /* y = z + z * (zz * polevl( zz, sincof, 5 ));*/
  321. y = z + z * z * z * polevl( zz, sincof, 5 );
  322. }
  323. else
  324. {
  325. y = 1.0 - ldexp(zz,-1) + zz * zz * polevl( zz, coscof, 5 );
  326. }
  327. if(sign < 0)
  328. y = -y;
  329. return(y);
  330. }
  331. /* Degrees, minutes, seconds to radians: */
  332. /* 1 arc second, in radians = 4.8481368110953599358991410e-5 */
  333. #ifdef DEC
  334. static unsigned short P648[] = {034513,054170,0176773,0116043,};
  335. #define P64800 *(double *)P648
  336. #else
  337. static double P64800 = 4.8481368110953599358991410e-5;
  338. #endif
  339. double radian(d,m,s)
  340. double d,m,s;
  341. {
  342. return( ((d*60.0 + m)*60.0 + s)*P64800 );
  343. }