xref: /csrg-svn/lib/libm/common/trig.c (revision 31851)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)trig.c	1.2 (Berkeley) 8/22/85; 1.6 (ucb.elefunt) 07/13/87";
17 #endif not lint
18 
19 /* SIN(X), COS(X), TAN(X)
20  * RETURN THE SINE, COSINE, AND TANGENT OF X RESPECTIVELY
21  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
22  * CODED IN C BY K.C. NG, 1/8/85;
23  * REVISED BY W. Kahan and K.C. NG, 8/17/85.
24  *
25  * Required system supported functions:
26  *      copysign(x,y)
27  *      finite(x)
28  *      drem(x,p)
29  *
30  * Static kernel functions:
31  *      sin__S(z)       ....sin__S(x*x) return (sin(x)-x)/x
32  *      cos__C(z)       ....cos__C(x*x) return cos(x)-1-x*x/2
33  *
34  * Method.
35  *      Let S and C denote the polynomial approximations to sin and cos
36  *      respectively on [-PI/4, +PI/4].
37  *
38  *      SIN and COS:
39  *      1. Reduce the argument into [-PI , +PI] by the remainder function.
40  *      2. For x in (-PI,+PI), there are three cases:
41  *			case 1:	|x| < PI/4
42  *			case 2:	PI/4 <= |x| < 3PI/4
43  *			case 3:	3PI/4 <= |x|.
44  *	   SIN and COS of x are computed by:
45  *
46  *                   sin(x)      cos(x)       remark
47  *     ----------------------------------------------------------
48  *        case 1     S(x)         C(x)
49  *        case 2 sign(x)*C(y)     S(y)      y=PI/2-|x|
50  *        case 3     S(y)        -C(y)      y=sign(x)*(PI-|x|)
51  *     ----------------------------------------------------------
52  *
53  *      TAN:
54  *      1. Reduce the argument into [-PI/2 , +PI/2] by the remainder function.
55  *      2. For x in (-PI/2,+PI/2), there are two cases:
56  *			case 1:	|x| < PI/4
57  *			case 2:	PI/4 <= |x| < PI/2
58  *         TAN of x is computed by:
59  *
60  *                   tan (x)            remark
61  *     ----------------------------------------------------------
62  *        case 1     S(x)/C(x)
63  *        case 2     C(y)/S(y)     y=sign(x)*(PI/2-|x|)
64  *     ----------------------------------------------------------
65  *
66  *   Notes:
67  *      1. S(y) and C(y) were computed by:
68  *              S(y) = y+y*sin__S(y*y)
69  *              C(y) = 1-(y*y/2-cos__C(x*x))          ... if y*y/2 <  thresh,
70  *                   = 0.5-((y*y/2-0.5)-cos__C(x*x))  ... if y*y/2 >= thresh.
71  *         where
72  *              thresh = 0.5*(acos(3/4)**2)
73  *
74  *      2. For better accuracy, we use the following formula for S/C for tan
75  *         (k=0): let ss=sin__S(y*y), and cc=cos__C(y*y), then
76  *
77  *                            y+y*ss             (y*y/2-cc)+ss
78  *             S(y)/C(y)   = -------- = y + y * ---------------.
79  *                               C                     C
80  *
81  *
82  * Special cases:
83  *      Let trig be any of sin, cos, or tan.
84  *      trig(+-INF)  is NaN, with signals;
85  *      trig(NaN)    is that NaN;
86  *      trig(n*PI/2) is exact for any integer n, provided n*PI is
87  *      representable; otherwise, trig(x) is inexact.
88  *
89  * Accuracy:
90  *      trig(x) returns the exact trig(x*pi/PI) nearly rounded, where
91  *
92  *      Decimal:
93  *              pi = 3.141592653589793 23846264338327 .....
94  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
95  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
96  *
97  *      Hexadecimal:
98  *              pi = 3.243F6A8885A308D313198A2E....
99  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18    error=.276ulps
100  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
101  *
102  *      In a test run with 1,024,000 random arguments on a VAX, the maximum
103  *      observed errors (compared with the exact trig(x*pi/PI)) were
104  *                      tan(x) : 2.09 ulps (around 4.716340404662354)
105  *                      sin(x) : .861 ulps
106  *                      cos(x) : .857 ulps
107  *
108  * Constants:
109  * The hexadecimal values are the intended ones for the following constants.
110  * The decimal values may be used, provided that the compiler will convert
111  * from decimal to binary accurately enough to produce the hexadecimal values
112  * shown.
113  */
114 
115 #if (defined(VAX)||defined(TAHOE))
116 #ifdef VAX
117 #define _0x(A,B)	0x/**/A/**/B
118 #else	/* VAX */
119 #define _0x(A,B)	0x/**/B/**/A
120 #endif	/* VAX */
121 /*thresh =  2.6117239648121182150E-1    , Hex  2^ -1   *  .85B8636B026EA0 */
122 /*PIo4   =  7.8539816339744830676E-1    , Hex  2^  0   *  .C90FDAA22168C2 */
123 /*PIo2   =  1.5707963267948966135E0     , Hex  2^  1   *  .C90FDAA22168C2 */
124 /*PI3o4  =  2.3561944901923449203E0     , Hex  2^  2   *  .96CBE3F9990E92 */
125 /*PI     =  3.1415926535897932270E0     , Hex  2^  2   *  .C90FDAA22168C2 */
126 /*PI2    =  6.2831853071795864540E0     ; Hex  2^  3   *  .C90FDAA22168C2 */
127 static long    threshx[] = { _0x(b863,3f85), _0x(6ea0,6b02)};
128 #define   thresh    (*(double*)threshx)
129 static long      PIo4x[] = { _0x(0fda,4049), _0x(68c2,a221)};
130 #define     PIo4    (*(double*)PIo4x)
131 static long      PIo2x[] = { _0x(0fda,40c9), _0x(68c2,a221)};
132 #define     PIo2    (*(double*)PIo2x)
133 static long      PI3o4x[] = { _0x(cbe3,4116), _0x(0e92,f999)};
134 #define     PI3o4    (*(double*)PI3o4x)
135 static long        PIx[] = { _0x(0fda,4149), _0x(68c2,a221)};
136 #define       PI    (*(double*)PIx)
137 static long       PI2x[] = { _0x(0fda,41c9), _0x(68c2,a221)};
138 #define      PI2    (*(double*)PI2x)
139 #else   /* IEEE double  */
140 static double
141 thresh =  2.6117239648121182150E-1    , /*Hex  2^ -2   *  1.0B70C6D604DD4 */
142 PIo4   =  7.8539816339744827900E-1    , /*Hex  2^ -1   *  1.921FB54442D18 */
143 PIo2   =  1.5707963267948965580E0     , /*Hex  2^  0   *  1.921FB54442D18 */
144 PI3o4  =  2.3561944901923448370E0     , /*Hex  2^  1   *  1.2D97C7F3321D2 */
145 PI     =  3.1415926535897931160E0     , /*Hex  2^  1   *  1.921FB54442D18 */
146 PI2    =  6.2831853071795862320E0     ; /*Hex  2^  2   *  1.921FB54442D18 */
147 #ifdef NATIONAL
148 static long    fmaxx[] = { 0xffffffff, 0x7fefffff};
149 #define   fmax    (*(double*)fmaxx)
150 #endif	/* NATIONAL */
151 #endif
152 static double zero=0, one=1, negone= -1, half=1.0/2.0,
153 	      small=1E-10, /* 1+small**2==1; better values for small:
154 					small = 1.5E-9 for VAX D
155 					      = 1.2E-8 for IEEE Double
156 					      = 2.8E-10 for IEEE Extended */
157 	      big=1E20;    /* big = 1/(small**2) */
158 
159 double tan(x)
160 double x;
161 {
162         double copysign(),drem(),cos__C(),sin__S(),a,z,ss,cc,c;
163 	int k;
164 #if (!defined(VAX)&&!defined(TAHOE))
165         extern int finite();
166         /* tan(NaN) and tan(INF) must be NaN */
167             if(!finite(x))  return(x-x);
168 #endif
169         x=drem(x,PI);        /* reduce x into [-PI/2, PI/2] */
170         a=copysign(x,one);   /* ... = abs(x) */
171 	if ( a >= PIo4 ) {k=1; x = copysign( PIo2 - a , x ); }
172 	   else { k=0; if(a < small ) { big + a; return(x); }}
173 
174         z  = x*x;
175         cc = cos__C(z);
176         ss = sin__S(z);
177 	z  = z*half ;		/* Next get c = cos(x) accurately */
178 	c  = (z >= thresh )? half-((z-half)-cc) : one-(z-cc);
179 	if (k==0) return ( x + (x*(z-(cc-ss)))/c );  /* sin/cos */
180 #ifdef NATIONAL
181 	else if(x==0.0) return copysign(fmax,x);  /* no inf on 32k */
182 #endif	/* NATIONAL */
183 	else return( c/(x+x*ss) );	/*          ... cos/sin */
184 
185 
186 }
187 double sin(x)
188 double x;
189 {
190         double copysign(),drem(),sin__S(),cos__C(),a,c,z;
191 #if (!defined(VAX)&&!defined(TAHOE))
192         extern int finite();
193         /* sin(NaN) and sin(INF) must be NaN */
194             if(!finite(x))  return(x-x);
195 #endif
196 	x=drem(x,PI2);         /*    reduce x into [-PI, PI] */
197         a=copysign(x,one);
198 	if( a >= PIo4 ) {
199 	     if( a >= PI3o4 )   /* 	.. in [3PI/4,  PI ]  */
200 		x=copysign((a=PI-a),x);
201 
202 	     else {	       /* 	.. in [PI/4, 3PI/4]  */
203 		a=PIo2-a;      /* return sign(x)*C(PI/2-|x|) */
204 		z=a*a;
205 		c=cos__C(z);
206 		z=z*half;
207 		a=(z>=thresh)?half-((z-half)-c):one-(z-c);
208 		return(copysign(a,x));
209 		}
210              }
211 
212         /* return S(x) */
213             if( a < small) { big + a; return(x);}
214             return(x+x*sin__S(x*x));
215 }
216 
217 double cos(x)
218 double x;
219 {
220         double copysign(),drem(),sin__S(),cos__C(),a,c,z,s=1.0;
221 #if (!defined(VAX)&&!defined(TAHOE))
222         extern int finite();
223         /* cos(NaN) and cos(INF) must be NaN */
224             if(!finite(x))  return(x-x);
225 #endif
226 	x=drem(x,PI2);         /*    reduce x into [-PI, PI] */
227         a=copysign(x,one);
228 	if ( a >= PIo4 ) {
229 	     if ( a >= PI3o4 )  /* 	.. in [3PI/4,  PI ]  */
230 		{ a=PI-a; s= negone; }
231 
232 	     else 	       /* 	.. in [PI/4, 3PI/4]  */
233                                /*        return  S(PI/2-|x|) */
234 		{ a=PIo2-a; return(a+a*sin__S(a*a));}
235 	     }
236 
237 
238         /* return s*C(a) */
239             if( a < small) { big + a; return(s);}
240 	    z=a*a;
241 	    c=cos__C(z);
242 	    z=z*half;
243 	    a=(z>=thresh)?half-((z-half)-c):one-(z-c);
244 	    return(copysign(a,s));
245 }
246 
247 
248 /* sin__S(x*x)
249  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
250  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
251  * CODED IN C BY K.C. NG, 1/21/85;
252  * REVISED BY K.C. NG on 8/13/85.
253  *
254  *	    sin(x*k) - x
255  * RETURN  --------------- on [-PI/4,PI/4] , where k=pi/PI, PI is the rounded
256  *	            x
257  * value of pi in machine precision:
258  *
259  *	Decimal:
260  *		pi = 3.141592653589793 23846264338327 .....
261  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
262  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
263  *
264  *	Hexadecimal:
265  *		pi = 3.243F6A8885A308D313198A2E....
266  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
267  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
268  *
269  * Method:
270  *	1. Let z=x*x. Create a polynomial approximation to
271  *	    (sin(k*x)-x)/x  =  z*(S0 + S1*z^1 + ... + S5*z^5).
272  *	Then
273  *      sin__S(x*x) = z*(S0 + S1*z^1 + ... + S5*z^5)
274  *
275  *	The coefficient S's are obtained by a special Remez algorithm.
276  *
277  * Accuracy:
278  *	In the absence of rounding error, the approximation has absolute error
279  *	less than 2**(-61.11) for VAX D FORMAT, 2**(-57.45) for IEEE DOUBLE.
280  *
281  * Constants:
282  * The hexadecimal values are the intended ones for the following constants.
283  * The decimal values may be used, provided that the compiler will convert
284  * from decimal to binary accurately enough to produce the hexadecimal values
285  * shown.
286  *
287  */
288 
289 #if (defined(VAX)||defined(TAHOE))
290 /*S0     = -1.6666666666666646660E-1    , Hex  2^ -2   * -.AAAAAAAAAAAA71 */
291 /*S1     =  8.3333333333297230413E-3    , Hex  2^ -6   *  .8888888888477F */
292 /*S2     = -1.9841269838362403710E-4    , Hex  2^-12   * -.D00D00CF8A1057 */
293 /*S3     =  2.7557318019967078930E-6    , Hex  2^-18   *  .B8EF1CA326BEDC */
294 /*S4     = -2.5051841873876551398E-8    , Hex  2^-25   * -.D73195374CE1D3 */
295 /*S5     =  1.6028995389845827653E-10   , Hex  2^-32   *  .B03D9C6D26CCCC */
296 /*S6     = -6.2723499671769283121E-13   ; Hex  2^-40   * -.B08D0B7561EA82 */
297 static long        S0x[] = { _0x(aaaa,bf2a), _0x(aa71,aaaa)};
298 #define       S0    (*(double*)S0x)
299 static long        S1x[] = { _0x(8888,3d08), _0x(477f,8888)};
300 #define       S1    (*(double*)S1x)
301 static long        S2x[] = { _0x(0d00,ba50), _0x(1057,cf8a)};
302 #define       S2    (*(double*)S2x)
303 static long        S3x[] = { _0x(ef1c,3738), _0x(bedc,a326)};
304 #define       S3    (*(double*)S3x)
305 static long        S4x[] = { _0x(3195,b3d7), _0x(e1d3,374c)};
306 #define       S4    (*(double*)S4x)
307 static long        S5x[] = { _0x(3d9c,3030), _0x(cccc,6d26)};
308 #define       S5    (*(double*)S5x)
309 static long        S6x[] = { _0x(8d0b,ac30), _0x(ea82,7561)};
310 #define       S6    (*(double*)S6x)
311 #else	/* IEEE double  */
312 static double
313 S0     = -1.6666666666666463126E-1    , /*Hex  2^ -3   * -1.555555555550C */
314 S1     =  8.3333333332992771264E-3    , /*Hex  2^ -7   *  1.111111110C461 */
315 S2     = -1.9841269816180999116E-4    , /*Hex  2^-13   * -1.A01A019746345 */
316 S3     =  2.7557309793219876880E-6    , /*Hex  2^-19   *  1.71DE3209CDCD9 */
317 S4     = -2.5050225177523807003E-8    , /*Hex  2^-26   * -1.AE5C0E319A4EF */
318 S5     =  1.5868926979889205164E-10   ; /*Hex  2^-33   *  1.5CF61DF672B13 */
319 #endif
320 
321 static double sin__S(z)
322 double z;
323 {
324 #if (defined(VAX)||defined(TAHOE))
325 	return(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*(S5+z*S6)))))));
326 #else 	/* IEEE double */
327 	return(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*S5))))));
328 #endif
329 }
330 
331 
332 /* cos__C(x*x)
333  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
334  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
335  * CODED IN C BY K.C. NG, 1/21/85;
336  * REVISED BY K.C. NG on 8/13/85.
337  *
338  *	   		    x*x
339  * RETURN   cos(k*x) - 1 + ----- on [-PI/4,PI/4],  where k = pi/PI,
340  *	  		     2
341  * PI is the rounded value of pi in machine precision :
342  *
343  *	Decimal:
344  *		pi = 3.141592653589793 23846264338327 .....
345  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
346  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
347  *
348  *	Hexadecimal:
349  *		pi = 3.243F6A8885A308D313198A2E....
350  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
351  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
352  *
353  *
354  * Method:
355  *	1. Let z=x*x. Create a polynomial approximation to
356  *	    cos(k*x)-1+z/2  =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
357  *	then
358  *      cos__C(z) =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
359  *
360  *	The coefficient C's are obtained by a special Remez algorithm.
361  *
362  * Accuracy:
363  *	In the absence of rounding error, the approximation has absolute error
364  *	less than 2**(-64) for VAX D FORMAT, 2**(-58.3) for IEEE DOUBLE.
365  *
366  *
367  * Constants:
368  * The hexadecimal values are the intended ones for the following constants.
369  * The decimal values may be used, provided that the compiler will convert
370  * from decimal to binary accurately enough to produce the hexadecimal values
371  * shown.
372  *
373  */
374 
375 #if (defined(VAX)||defined(TAHOE))
376 /*C0     =  4.1666666666666504759E-2    , Hex  2^ -4   *  .AAAAAAAAAAA9F0 */
377 /*C1     = -1.3888888888865302059E-3    , Hex  2^ -9   * -.B60B60B60A0CCA */
378 /*C2     =  2.4801587285601038265E-5    , Hex  2^-15   *  .D00D00CDCD098F */
379 /*C3     = -2.7557313470902390219E-7    , Hex  2^-21   * -.93F27BB593E805 */
380 /*C4     =  2.0875623401082232009E-9    , Hex  2^-28   *  .8F74C8FA1E3FF0 */
381 /*C5     = -1.1355178117642986178E-11   ; Hex  2^-36   * -.C7C32D0A5C5A63 */
382 static long        C0x[] = { _0x(aaaa,3e2a), _0x(a9f0,aaaa)};
383 #define       C0    (*(double*)C0x)
384 static long        C1x[] = { _0x(0b60,bbb6), _0x(0cca,b60a)};
385 #define       C1    (*(double*)C1x)
386 static long        C2x[] = { _0x(0d00,38d0), _0x(098f,cdcd)};
387 #define       C2    (*(double*)C2x)
388 static long        C3x[] = { _0x(f27b,b593), _0x(e805,b593)};
389 #define       C3    (*(double*)C3x)
390 static long        C4x[] = { _0x(74c8,320f), _0x(3ff0,fa1e)};
391 #define       C4    (*(double*)C4x)
392 static long        C5x[] = { _0x(c32d,ae47), _0x(5a63,0a5c)};
393 #define       C5    (*(double*)C5x)
394 #else	/* IEEE double  */
395 static double
396 C0     =  4.1666666666666504759E-2    , /*Hex  2^ -5   *  1.555555555553E */
397 C1     = -1.3888888888865301516E-3    , /*Hex  2^-10   * -1.6C16C16C14199 */
398 C2     =  2.4801587269650015769E-5    , /*Hex  2^-16   *  1.A01A01971CAEB */
399 C3     = -2.7557304623183959811E-7    , /*Hex  2^-22   * -1.27E4F1314AD1A */
400 C4     =  2.0873958177697780076E-9    , /*Hex  2^-29   *  1.1EE3B60DDDC8C */
401 C5     = -1.1250289076471311557E-11   ; /*Hex  2^-37   * -1.8BD5986B2A52E */
402 #endif
403 
404 static double cos__C(z)
405 double z;
406 {
407 	return(z*z*(C0+z*(C1+z*(C2+z*(C3+z*(C4+z*C5))))));
408 }
409