sincos.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* sincos.c
  2. *
  3. * Circular sine and cosine of argument in degrees
  4. * Table lookup and interpolation algorithm
  5. *
  6. *
  7. *
  8. * SYNOPSIS:
  9. *
  10. * double x, sine, cosine, flg, sincos();
  11. *
  12. * sincos( x, &sine, &cosine, flg );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns both the sine and the cosine of the argument x.
  19. * Several different compile time options and minimax
  20. * approximations are supplied to permit tailoring the
  21. * tradeoff between computation speed and accuracy.
  22. *
  23. * Since range reduction is time consuming, the reduction
  24. * of x modulo 360 degrees is also made optional.
  25. *
  26. * sin(i) is internally tabulated for 0 <= i <= 90 degrees.
  27. * Approximation polynomials, ranging from linear interpolation
  28. * to cubics in (x-i)**2, compute the sine and cosine
  29. * of the residual x-i which is between -0.5 and +0.5 degree.
  30. * In the case of the high accuracy options, the residual
  31. * and the tabulated values are combined using the trigonometry
  32. * formulas for sin(A+B) and cos(A+B).
  33. *
  34. * Compile time options are supplied for 5, 11, or 17 decimal
  35. * relative accuracy (ACC5, ACC11, ACC17 respectively).
  36. * A subroutine flag argument "flg" chooses betwen this
  37. * accuracy and table lookup only (peak absolute error
  38. * = 0.0087).
  39. *
  40. * If the argument flg = 1, then the tabulated value is
  41. * returned for the nearest whole number of degrees. The
  42. * approximation polynomials are not computed. At
  43. * x = 0.5 deg, the absolute error is then sin(0.5) = 0.0087.
  44. *
  45. * An intermediate speed and precision can be obtained using
  46. * the compile time option LINTERP and flg = 1. This yields
  47. * a linear interpolation using a slope estimated from the sine
  48. * or cosine at the nearest integer argument. The peak absolute
  49. * error with this option is 3.8e-5. Relative error at small
  50. * angles is about 1e-5.
  51. *
  52. * If flg = 0, then the approximation polynomials are computed
  53. * and applied.
  54. *
  55. *
  56. *
  57. * SPEED:
  58. *
  59. * Relative speed comparisons follow for 6MHz IBM AT clone
  60. * and Microsoft C version 4.0. These figures include
  61. * software overhead of do loop and function calls.
  62. * Since system hardware and software vary widely, the
  63. * numbers should be taken as representative only.
  64. *
  65. * flg=0 flg=0 flg=1 flg=1
  66. * ACC11 ACC5 LINTERP Lookup only
  67. * In-line 8087 (/FPi)
  68. * sin(), cos() 1.0 1.0 1.0 1.0
  69. *
  70. * In-line 8087 (/FPi)
  71. * sincos() 1.1 1.4 1.9 3.0
  72. *
  73. * Software (/FPa)
  74. * sin(), cos() 0.19 0.19 0.19 0.19
  75. *
  76. * Software (/FPa)
  77. * sincos() 0.39 0.50 0.73 1.7
  78. *
  79. *
  80. *
  81. * ACCURACY:
  82. *
  83. * The accurate approximations are designed with a relative error
  84. * criterion. The absolute error is greatest at x = 0.5 degree.
  85. * It decreases from a local maximum at i+0.5 degrees to full
  86. * machine precision at each integer i degrees. With the
  87. * ACC5 option, the relative error of 6.3e-6 is equivalent to
  88. * an absolute angular error of 0.01 arc second in the argument
  89. * at x = i+0.5 degrees. For small angles < 0.5 deg, the ACC5
  90. * accuracy is 6.3e-6 (.00063%) of reading; i.e., the absolute
  91. * error decreases in proportion to the argument. This is true
  92. * for both the sine and cosine approximations, since the latter
  93. * is for the function 1 - cos(x).
  94. *
  95. * If absolute error is of most concern, use the compile time
  96. * option ABSERR to obtain an absolute error of 2.7e-8 for ACC5
  97. * precision. This is about half the absolute error of the
  98. * relative precision option. In this case the relative error
  99. * for small angles will increase to 9.5e-6 -- a reasonable
  100. * tradeoff.
  101. */
  102. #include "mconf.h"
  103. /* Define one of the following to be 1:
  104. */
  105. #define ACC5 1
  106. #define ACC11 0
  107. #define ACC17 0
  108. /* Option for linear interpolation when flg = 1
  109. */
  110. #define LINTERP 1
  111. /* Option for absolute error criterion
  112. */
  113. #define ABSERR 1
  114. /* Option to include modulo 360 function:
  115. */
  116. #define MOD360 1
  117. /*
  118. Cephes Math Library Release 2.1
  119. Copyright 1987 by Stephen L. Moshier
  120. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  121. */
  122. /* Table of sin(i degrees)
  123. * for 0 <= i <= 90
  124. */
  125. static double sintbl[92] = {
  126. 0.00000000000000000000E0, 1.74524064372835128194E-2,
  127. 3.48994967025009716460E-2, 5.23359562429438327221E-2,
  128. 6.97564737441253007760E-2, 8.71557427476581735581E-2,
  129. 1.04528463267653471400E-1, 1.21869343405147481113E-1,
  130. 1.39173100960065444112E-1, 1.56434465040230869010E-1,
  131. 1.73648177666930348852E-1, 1.90808995376544812405E-1,
  132. 2.07911690817759337102E-1, 2.24951054343864998051E-1,
  133. 2.41921895599667722560E-1, 2.58819045102520762349E-1,
  134. 2.75637355816999185650E-1, 2.92371704722736728097E-1,
  135. 3.09016994374947424102E-1, 3.25568154457156668714E-1,
  136. 3.42020143325668733044E-1, 3.58367949545300273484E-1,
  137. 3.74606593415912035415E-1, 3.90731128489273755062E-1,
  138. 4.06736643075800207754E-1, 4.22618261740699436187E-1,
  139. 4.38371146789077417453E-1, 4.53990499739546791560E-1,
  140. 4.69471562785890775959E-1, 4.84809620246337029075E-1,
  141. 5.00000000000000000000E-1, 5.15038074910054210082E-1,
  142. 5.29919264233204954047E-1, 5.44639035015027082224E-1,
  143. 5.59192903470746830160E-1, 5.73576436351046096108E-1,
  144. 5.87785252292473129169E-1, 6.01815023152048279918E-1,
  145. 6.15661475325658279669E-1, 6.29320391049837452706E-1,
  146. 6.42787609686539326323E-1, 6.56059028990507284782E-1,
  147. 6.69130606358858213826E-1, 6.81998360062498500442E-1,
  148. 6.94658370458997286656E-1, 7.07106781186547524401E-1,
  149. 7.19339800338651139356E-1, 7.31353701619170483288E-1,
  150. 7.43144825477394235015E-1, 7.54709580222771997943E-1,
  151. 7.66044443118978035202E-1, 7.77145961456970879980E-1,
  152. 7.88010753606721956694E-1, 7.98635510047292846284E-1,
  153. 8.09016994374947424102E-1, 8.19152044288991789684E-1,
  154. 8.29037572555041692006E-1, 8.38670567945424029638E-1,
  155. 8.48048096156425970386E-1, 8.57167300702112287465E-1,
  156. 8.66025403784438646764E-1, 8.74619707139395800285E-1,
  157. 8.82947592858926942032E-1, 8.91006524188367862360E-1,
  158. 8.98794046299166992782E-1, 9.06307787036649963243E-1,
  159. 9.13545457642600895502E-1, 9.20504853452440327397E-1,
  160. 9.27183854566787400806E-1, 9.33580426497201748990E-1,
  161. 9.39692620785908384054E-1, 9.45518575599316810348E-1,
  162. 9.51056516295153572116E-1, 9.56304755963035481339E-1,
  163. 9.61261695938318861916E-1, 9.65925826289068286750E-1,
  164. 9.70295726275996472306E-1, 9.74370064785235228540E-1,
  165. 9.78147600733805637929E-1, 9.81627183447663953497E-1,
  166. 9.84807753012208059367E-1, 9.87688340595137726190E-1,
  167. 9.90268068741570315084E-1, 9.92546151641322034980E-1,
  168. 9.94521895368273336923E-1, 9.96194698091745532295E-1,
  169. 9.97564050259824247613E-1, 9.98629534754573873784E-1,
  170. 9.99390827019095730006E-1, 9.99847695156391239157E-1,
  171. 1.00000000000000000000E0, 9.99847695156391239157E-1,
  172. };
  173. #ifdef ANSIPROT
  174. double floor(double);
  175. #else
  176. double floor();
  177. #endif
  178. int sincos(x, s, c, flg) double x;
  179. double *s, *c;
  180. int flg;
  181. {
  182. int ix, ssign, csign, xsign;
  183. double y, z, sx, sz, cx, cz;
  184. /* Make argument nonnegative.
  185. */
  186. xsign = 1;
  187. if (x < 0.0) {
  188. xsign = -1;
  189. x = -x;
  190. }
  191. #if MOD360
  192. x = x - 360.0 * floor(x / 360.0);
  193. #endif
  194. /* Find nearest integer to x.
  195. * Note there should be a domain error test here,
  196. * but this is omitted to gain speed.
  197. */
  198. ix = x + 0.5;
  199. z = x - ix; /* the residual */
  200. /* Look up the sine and cosine of the integer.
  201. */
  202. if (ix <= 180) {
  203. ssign = 1;
  204. csign = 1;
  205. } else {
  206. ssign = -1;
  207. csign = -1;
  208. ix -= 180;
  209. }
  210. if (ix > 90) {
  211. csign = -csign;
  212. ix = 180 - ix;
  213. }
  214. sx = sintbl[ix];
  215. if (ssign < 0)
  216. sx = -sx;
  217. cx = sintbl[90 - ix];
  218. if (csign < 0)
  219. cx = -cx;
  220. /* If the flag argument is set, then just return
  221. * the tabulated values for arg to the nearest whole degree.
  222. */
  223. if (flg) {
  224. #if LINTERP
  225. y = sx + 1.74531263774940077459e-2 * z * cx;
  226. cx -= 1.74531263774940077459e-2 * z * sx;
  227. sx = y;
  228. #endif
  229. if (xsign < 0)
  230. sx = -sx;
  231. *s = sx; /* sine */
  232. *c = cx; /* cosine */
  233. return 0;
  234. }
  235. /* Find sine and cosine
  236. * of the residual angle between -0.5 and +0.5 degree.
  237. */
  238. #if ACC5
  239. #if ABSERR
  240. /* absolute error = 2.769e-8: */
  241. sz = 1.74531263774940077459e-2 * z;
  242. /* absolute error = 4.146e-11: */
  243. cz = 1.0 - 1.52307909153324666207e-4 * z * z;
  244. #else
  245. /* relative error = 6.346e-6: */
  246. sz = 1.74531817576426662296e-2 * z;
  247. /* relative error = 3.173e-6: */
  248. cz = 1.0 - 1.52308226602566149927e-4 * z * z;
  249. #endif
  250. #else
  251. y = z * z;
  252. #endif
  253. #if ACC11
  254. sz = (-8.86092781698004819918e-7 * y + 1.74532925198378577601e-2) * z;
  255. cz = 1.0 - (-3.86631403698859047896e-9 * y + 1.52308709893047593702e-4) * y;
  256. #endif
  257. #if ACC17
  258. sz = ((1.34959795251974073996e-11 * y - 8.86096155697856783296e-7) * y +
  259. 1.74532925199432957214e-2) *
  260. z;
  261. cz = 1.0 - ((3.92582397764340914444e-14 * y - 3.86632385155548605680e-9) * y +
  262. 1.52308709893354299569e-4) *
  263. y;
  264. #endif
  265. /* Combine the tabulated part and the calculated part
  266. * by trigonometry.
  267. */
  268. y = sx * cz + cx * sz;
  269. if (xsign < 0)
  270. y = -y;
  271. *s = y; /* sine */
  272. *c = cx * cz - sx * sz; /* cosine */
  273. return 0;
  274. }