sindg.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* sindg.c
  2. *
  3. * Circular sine of angle in degrees
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, sindg();
  10. *
  11. * y = sindg( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Range reduction is into intervals of 45 degrees.
  18. *
  19. * Two polynomial approximating functions are employed.
  20. * Between 0 and pi/4 the sine is approximated by
  21. * x + x**3 P(x**2).
  22. * Between pi/4 and pi/2 the cosine is represented as
  23. * 1 - x**2 P(x**2).
  24. *
  25. *
  26. *
  27. * ACCURACY:
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * DEC +-1000 3100 3.3e-17 9.0e-18
  32. * IEEE +-1000 30000 2.3e-16 5.6e-17
  33. *
  34. * ERROR MESSAGES:
  35. *
  36. * message condition value returned
  37. * sindg total loss x > 8.0e14 (DEC) 0.0
  38. * x > 1.0e14 (IEEE)
  39. *
  40. */
  41. /* cosdg.c
  42. *
  43. * Circular cosine of angle in degrees
  44. *
  45. *
  46. *
  47. * SYNOPSIS:
  48. *
  49. * double x, y, cosdg();
  50. *
  51. * y = cosdg( x );
  52. *
  53. *
  54. *
  55. * DESCRIPTION:
  56. *
  57. * Range reduction is into intervals of 45 degrees.
  58. *
  59. * Two polynomial approximating functions are employed.
  60. * Between 0 and pi/4 the cosine is approximated by
  61. * 1 - x**2 P(x**2).
  62. * Between pi/4 and pi/2 the sine is represented as
  63. * x + x**3 P(x**2).
  64. *
  65. *
  66. * ACCURACY:
  67. *
  68. * Relative error:
  69. * arithmetic domain # trials peak rms
  70. * DEC +-1000 3400 3.5e-17 9.1e-18
  71. * IEEE +-1000 30000 2.1e-16 5.7e-17
  72. * See also sin().
  73. *
  74. */
  75. /* Cephes Math Library Release 2.0: April, 1987
  76. * Copyright 1985, 1987 by Stephen L. Moshier
  77. * Direct inquiries to 30 Frost Street, Cambridge, MA 02140 */
  78. #include <math.h>
  79. #ifdef UNK
  80. static double sincof[] = {
  81. 1.58962301572218447952E-10,
  82. -2.50507477628503540135E-8,
  83. 2.75573136213856773549E-6,
  84. -1.98412698295895384658E-4,
  85. 8.33333333332211858862E-3,
  86. -1.66666666666666307295E-1
  87. };
  88. static double coscof[] = {
  89. 1.13678171382044553091E-11,
  90. -2.08758833757683644217E-9,
  91. 2.75573155429816611547E-7,
  92. -2.48015872936186303776E-5,
  93. 1.38888888888806666760E-3,
  94. -4.16666666666666348141E-2,
  95. 4.99999999999999999798E-1
  96. };
  97. static double PI180 = 1.74532925199432957692E-2; /* pi/180 */
  98. static double lossth = 1.0e14;
  99. #endif
  100. #ifdef DEC
  101. static unsigned short sincof[] = {
  102. 0030056,0143750,0177170,0073013,
  103. 0131727,0027455,0044510,0132205,
  104. 0033470,0167432,0131752,0042263,
  105. 0135120,0006400,0146776,0174027,
  106. 0036410,0104210,0104207,0137202,
  107. 0137452,0125252,0125252,0125103
  108. };
  109. static unsigned short coscof[] = {
  110. 0027107,0176030,0153315,0110312,
  111. 0131017,0072476,0007450,0123243,
  112. 0032623,0171174,0070066,0146445,
  113. 0134320,0006400,0147355,0163313,
  114. 0035666,0005540,0133012,0165067,
  115. 0137052,0125252,0125252,0125206,
  116. 0040000,0000000,0000000,0000000
  117. };
  118. static unsigned short P1[] = {0036616,0175065,0011224,0164711};
  119. #define PI180 *(double *)P1
  120. static double lossth = 8.0e14;
  121. #endif
  122. #ifdef IBMPC
  123. static unsigned short sincof[] = {
  124. 0x0ec1,0x1fcf,0xd8fd,0x3de5,
  125. 0x1691,0xa929,0xe5e5,0xbe5a,
  126. 0x4896,0x567d,0x1de3,0x3ec7,
  127. 0xdf03,0x19bf,0x01a0,0xbf2a,
  128. 0xf7d0,0x1110,0x1111,0x3f81,
  129. 0x5548,0x5555,0x5555,0xbfc5
  130. };
  131. static unsigned short coscof[] = {
  132. 0xb219,0x1ad9,0xff83,0x3da8,
  133. 0x14d4,0xc1e5,0xeea7,0xbe21,
  134. 0xd9a5,0x8e06,0x7e4f,0x3e92,
  135. 0xbcd9,0x19dd,0x01a0,0xbefa,
  136. 0x5d47,0x16c1,0xc16c,0x3f56,
  137. 0x5551,0x5555,0x5555,0xbfa5,
  138. 0x0000,0x0000,0x0000,0x3fe0
  139. };
  140. static unsigned short P1[] = {0x9d39,0xa252,0xdf46,0x3f91};
  141. #define PI180 *(double *)P1
  142. static double lossth = 1.0e14;
  143. #endif
  144. #ifdef MIEEE
  145. static unsigned short sincof[] = {
  146. 0x3de5,0xd8fd,0x1fcf,0x0ec1,
  147. 0xbe5a,0xe5e5,0xa929,0x1691,
  148. 0x3ec7,0x1de3,0x567d,0x4896,
  149. 0xbf2a,0x01a0,0x19bf,0xdf03,
  150. 0x3f81,0x1111,0x1110,0xf7d0,
  151. 0xbfc5,0x5555,0x5555,0x5548
  152. };
  153. static unsigned short coscof[] = {
  154. 0x3da8,0xff83,0x1ad9,0xb219,
  155. 0xbe21,0xeea7,0xc1e5,0x14d4,
  156. 0x3e92,0x7e4f,0x8e06,0xd9a5,
  157. 0xbefa,0x01a0,0x19dd,0xbcd9,
  158. 0x3f56,0xc16c,0x16c1,0x5d47,
  159. 0xbfa5,0x5555,0x5555,0x5551,
  160. 0x3fe0,0x0000,0x0000,0x0000
  161. };
  162. static unsigned short P1[] = {
  163. 0x3f91,0xdf46,0xa252,0x9d39
  164. };
  165. #define PI180 *(double *)P1
  166. static double lossth = 1.0e14;
  167. #endif
  168. #ifdef ANSIPROT
  169. extern double polevl ( double, void *, int );
  170. extern double floor ( double );
  171. extern double ldexp ( double, int );
  172. #else
  173. double polevl(), floor(), ldexp();
  174. #endif
  175. extern double PIO4;
  176. double sindg(x)
  177. double x;
  178. {
  179. double y, z, zz;
  180. int j, sign;
  181. /* make argument positive but save the sign */
  182. sign = 1;
  183. if( x < 0 )
  184. {
  185. x = -x;
  186. sign = -1;
  187. }
  188. if( x > lossth )
  189. {
  190. mtherr( "sindg", TLOSS );
  191. return(0.0);
  192. }
  193. y = floor( x/45.0 ); /* integer part of x/PIO4 */
  194. /* strip high bits of integer part to prevent integer overflow */
  195. z = ldexp( y, -4 );
  196. z = floor(z); /* integer part of y/8 */
  197. z = y - ldexp( z, 4 ); /* y - 16 * (y/16) */
  198. j = z; /* convert to integer for tests on the phase angle */
  199. /* map zeros to origin */
  200. if( j & 1 )
  201. {
  202. j += 1;
  203. y += 1.0;
  204. }
  205. j = j & 07; /* octant modulo 360 degrees */
  206. /* reflect in x axis */
  207. if( j > 3)
  208. {
  209. sign = -sign;
  210. j -= 4;
  211. }
  212. z = x - y * 45.0; /* x mod 45 degrees */
  213. z *= PI180; /* multiply by pi/180 to convert to radians */
  214. zz = z * z;
  215. if( (j==1) || (j==2) )
  216. {
  217. y = 1.0 - zz * polevl( zz, coscof, 6 );
  218. }
  219. else
  220. {
  221. y = z + z * (zz * polevl( zz, sincof, 5 ));
  222. }
  223. if(sign < 0)
  224. y = -y;
  225. return(y);
  226. }
  227. double cosdg(x)
  228. double x;
  229. {
  230. double y, z, zz;
  231. int j, sign;
  232. /* make argument positive */
  233. sign = 1;
  234. if( x < 0 )
  235. x = -x;
  236. if( x > lossth )
  237. {
  238. mtherr( "cosdg", TLOSS );
  239. return(0.0);
  240. }
  241. y = floor( x/45.0 );
  242. z = ldexp( y, -4 );
  243. z = floor(z); /* integer part of y/8 */
  244. z = y - ldexp( z, 4 ); /* y - 16 * (y/16) */
  245. /* integer and fractional part modulo one octant */
  246. j = z;
  247. if( j & 1 ) /* map zeros to origin */
  248. {
  249. j += 1;
  250. y += 1.0;
  251. }
  252. j = j & 07;
  253. if( j > 3)
  254. {
  255. j -=4;
  256. sign = -sign;
  257. }
  258. if( j > 1 )
  259. sign = -sign;
  260. z = x - y * 45.0; /* x mod 45 degrees */
  261. z *= PI180; /* multiply by pi/180 to convert to radians */
  262. zz = z * z;
  263. if( (j==1) || (j==2) )
  264. {
  265. y = z + z * (zz * polevl( zz, sincof, 5 ));
  266. }
  267. else
  268. {
  269. y = 1.0 - zz * polevl( zz, coscof, 6 );
  270. }
  271. if(sign < 0)
  272. y = -y;
  273. return(y);
  274. }