tandg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* tandg.c
  2. *
  3. * Circular tangent of argument in degrees
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, tandg();
  10. *
  11. * y = tandg( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the circular tangent of the argument x in degrees.
  18. *
  19. * Range reduction is modulo pi/4. A rational function
  20. * x + x**3 P(x**2)/Q(x**2)
  21. * is employed in the basic interval [0, pi/4].
  22. *
  23. *
  24. *
  25. * ACCURACY:
  26. *
  27. * Relative error:
  28. * arithmetic domain # trials peak rms
  29. * DEC 0,10 8000 3.4e-17 1.2e-17
  30. * IEEE 0,10 30000 3.2e-16 8.4e-17
  31. *
  32. * ERROR MESSAGES:
  33. *
  34. * message condition value returned
  35. * tandg total loss x > 8.0e14 (DEC) 0.0
  36. * x > 1.0e14 (IEEE)
  37. * tandg singularity x = 180 k + 90 MAXNUM
  38. */
  39. /* cotdg.c
  40. *
  41. * Circular cotangent of argument in degrees
  42. *
  43. *
  44. *
  45. * SYNOPSIS:
  46. *
  47. * double x, y, cotdg();
  48. *
  49. * y = cotdg( x );
  50. *
  51. *
  52. *
  53. * DESCRIPTION:
  54. *
  55. * Returns the circular cotangent of the argument x in degrees.
  56. *
  57. * Range reduction is modulo pi/4. A rational function
  58. * x + x**3 P(x**2)/Q(x**2)
  59. * is employed in the basic interval [0, pi/4].
  60. *
  61. *
  62. * ERROR MESSAGES:
  63. *
  64. * message condition value returned
  65. * cotdg total loss x > 8.0e14 (DEC) 0.0
  66. * x > 1.0e14 (IEEE)
  67. * cotdg singularity x = 180 k MAXNUM
  68. */
  69. /*
  70. Cephes Math Library Release 2.8: June, 2000
  71. Copyright 1984, 1987, 2000 by Stephen L. Moshier
  72. */
  73. #include <math.h>
  74. #ifdef UNK
  75. static double P[] = {
  76. -1.30936939181383777646E4,
  77. 1.15351664838587416140E6,
  78. -1.79565251976484877988E7
  79. };
  80. static double Q[] = {
  81. /* 1.00000000000000000000E0,*/
  82. 1.36812963470692954678E4,
  83. -1.32089234440210967447E6,
  84. 2.50083801823357915839E7,
  85. -5.38695755929454629881E7
  86. };
  87. static double PI180 = 1.74532925199432957692E-2;
  88. static double lossth = 1.0e14;
  89. #endif
  90. #ifdef DEC
  91. static unsigned short P[] = {
  92. 0143514,0113306,0111171,0174674,
  93. 0045214,0147545,0027744,0167346,
  94. 0146210,0177526,0114514,0105660
  95. };
  96. static unsigned short Q[] = {
  97. /*0040200,0000000,0000000,0000000,*/
  98. 0043525,0142457,0072633,0025617,
  99. 0145241,0036742,0140525,0162256,
  100. 0046276,0146176,0013526,0143573,
  101. 0146515,0077401,0162762,0150607
  102. };
  103. static unsigned short P1[] = {0036616,0175065,0011224,0164711};
  104. #define PI180 *(double *)P1
  105. static double lossth = 8.0e14;
  106. #endif
  107. #ifdef IBMPC
  108. static unsigned short P[] = {
  109. 0x3f38,0xd24f,0x92d8,0xc0c9,
  110. 0x9ddd,0xa5fc,0x99ec,0x4131,
  111. 0x9176,0xd329,0x1fea,0xc171
  112. };
  113. static unsigned short Q[] = {
  114. /*0x0000,0x0000,0x0000,0x3ff0,*/
  115. 0x6572,0xeeb3,0xb8a5,0x40ca,
  116. 0xbc96,0x582a,0x27bc,0xc134,
  117. 0xd8ef,0xc2ea,0xd98f,0x4177,
  118. 0x5a31,0x3cbe,0xafe0,0xc189
  119. };
  120. static unsigned short P1[] = {0x9d39,0xa252,0xdf46,0x3f91};
  121. #define PI180 *(double *)P1
  122. static double lossth = 1.0e14;
  123. #endif
  124. #ifdef MIEEE
  125. static unsigned short P[] = {
  126. 0xc0c9,0x92d8,0xd24f,0x3f38,
  127. 0x4131,0x99ec,0xa5fc,0x9ddd,
  128. 0xc171,0x1fea,0xd329,0x9176
  129. };
  130. static unsigned short Q[] = {
  131. 0x40ca,0xb8a5,0xeeb3,0x6572,
  132. 0xc134,0x27bc,0x582a,0xbc96,
  133. 0x4177,0xd98f,0xc2ea,0xd8ef,
  134. 0xc189,0xafe0,0x3cbe,0x5a31
  135. };
  136. static unsigned short P1[] = {
  137. 0x3f91,0xdf46,0xa252,0x9d39
  138. };
  139. #define PI180 *(double *)P1
  140. static double lossth = 1.0e14;
  141. #endif
  142. #ifdef ANSIPROT
  143. extern double polevl ( double, void *, int );
  144. extern double p1evl ( double, void *, int );
  145. extern double floor ( double );
  146. extern double ldexp ( double, int );
  147. static double tancot( double, int );
  148. #else
  149. double polevl(), p1evl(), floor(), ldexp();
  150. static double tancot();
  151. #endif
  152. extern double MAXNUM;
  153. extern double PIO4;
  154. double tandg(x)
  155. double x;
  156. {
  157. return( tancot(x,0) );
  158. }
  159. double cotdg(x)
  160. double x;
  161. {
  162. return( tancot(x,1) );
  163. }
  164. static double tancot( xx, cotflg )
  165. double xx;
  166. int cotflg;
  167. {
  168. double x, y, z, zz;
  169. int j, sign;
  170. /* make argument positive but save the sign */
  171. if( xx < 0 )
  172. {
  173. x = -xx;
  174. sign = -1;
  175. }
  176. else
  177. {
  178. x = xx;
  179. sign = 1;
  180. }
  181. if( x > lossth )
  182. {
  183. mtherr( "tandg", TLOSS );
  184. return(0.0);
  185. }
  186. /* compute x mod PIO4 */
  187. y = floor( x/45.0 );
  188. /* strip high bits of integer part */
  189. z = ldexp( y, -3 );
  190. z = floor(z); /* integer part of y/8 */
  191. z = y - ldexp( z, 3 ); /* y - 16 * (y/16) */
  192. /* integer and fractional part modulo one octant */
  193. j = z;
  194. /* map zeros and singularities to origin */
  195. if( j & 1 )
  196. {
  197. j += 1;
  198. y += 1.0;
  199. }
  200. z = x - y * 45.0;
  201. z *= PI180;
  202. zz = z * z;
  203. if( zz > 1.0e-14 )
  204. y = z + z * (zz * polevl( zz, P, 2 )/p1evl(zz, Q, 4));
  205. else
  206. y = z;
  207. if( j & 2 )
  208. {
  209. if( cotflg )
  210. y = -y;
  211. else
  212. {
  213. if( y != 0.0 )
  214. {
  215. y = -1.0/y;
  216. }
  217. else
  218. {
  219. mtherr( "tandg", SING );
  220. y = MAXNUM;
  221. }
  222. }
  223. }
  224. else
  225. {
  226. if( cotflg )
  227. {
  228. if( y != 0.0 )
  229. y = 1.0/y;
  230. else
  231. {
  232. mtherr( "cotdg", SING );
  233. y = MAXNUM;
  234. }
  235. }
  236. }
  237. if( sign < 0 )
  238. y = -y;
  239. return( y );
  240. }