xref: /original-bsd/old/libm/libm/IEEE/trig.c (revision 1849f99b)
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[] = "@(#)trig.c	1.2 (Berkeley) 08/22/85";
16 #endif not lint
17 
18 /* SIN(X), COS(X), TAN(X)
19  * RETURN THE SINE, COSINE, AND TANGENT OF X RESPECTIVELY
20  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
21  * CODED IN C BY K.C. NG, 1/8/85;
22  * REVISED BY W. Kahan and K.C. NG, 8/17/85.
23  *
24  * Required system supported functions:
25  *      copysign(x,y)
26  *      finite(x)
27  *      drem(x,p)
28  *
29  * Static kernel functions:
30  *      sin__S(z)       ....sin__S(x*x) return (sin(x)-x)/x
31  *      cos__C(z)       ....cos__C(x*x) return cos(x)-1-x*x/2
32  *
33  * Method.
34  *      Let S and C denote the polynomial approximations to sin and cos
35  *      respectively on [-PI/4, +PI/4].
36  *
37  *      SIN and COS:
38  *      1. Reduce the argument into [-PI , +PI] by the remainder function.
39  *      2. For x in (-PI,+PI), there are three cases:
40  *			case 1:	|x| < PI/4
41  *			case 2:	PI/4 <= |x| < 3PI/4
42  *			case 3:	3PI/4 <= |x|.
43  *	   SIN and COS of x are computed by:
44  *
45  *                   sin(x)      cos(x)       remark
46  *     ----------------------------------------------------------
47  *        case 1     S(x)         C(x)
48  *        case 2 sign(x)*C(y)     S(y)      y=PI/2-|x|
49  *        case 3     S(y)        -C(y)      y=sign(x)*(PI-|x|)
50  *     ----------------------------------------------------------
51  *
52  *      TAN:
53  *      1. Reduce the argument into [-PI/2 , +PI/2] by the remainder function.
54  *      2. For x in (-PI/2,+PI/2), there are two cases:
55  *			case 1:	|x| < PI/4
56  *			case 2:	PI/4 <= |x| < PI/2
57  *         TAN of x is computed by:
58  *
59  *                   tan (x)            remark
60  *     ----------------------------------------------------------
61  *        case 1     S(x)/C(x)
62  *        case 2     C(y)/S(y)     y=sign(x)*(PI/2-|x|)
63  *     ----------------------------------------------------------
64  *
65  *   Notes:
66  *      1. S(y) and C(y) were computed by:
67  *              S(y) = y+y*sin__S(y*y)
68  *              C(y) = 1-(y*y/2-cos__C(x*x))          ... if y*y/2 <  thresh,
69  *                   = 0.5-((y*y/2-0.5)-cos__C(x*x))  ... if y*y/2 >= thresh.
70  *         where
71  *              thresh = 0.5*(acos(3/4)**2)
72  *
73  *      2. For better accuracy, we use the following formula for S/C for tan
74  *         (k=0): let ss=sin__S(y*y), and cc=cos__C(y*y), then
75  *
76  *                            y+y*ss             (y*y/2-cc)+ss
77  *             S(y)/C(y)   = -------- = y + y * ---------------.
78  *                               C                     C
79  *
80  *
81  * Special cases:
82  *      Let trig be any of sin, cos, or tan.
83  *      trig(+-INF)  is NaN, with signals;
84  *      trig(NaN)    is that NaN;
85  *      trig(n*PI/2) is exact for any integer n, provided n*PI is
86  *      representable; otherwise, trig(x) is inexact.
87  *
88  * Accuracy:
89  *      trig(x) returns the exact trig(x*pi/PI) nearly rounded, where
90  *
91  *      Decimal:
92  *              pi = 3.141592653589793 23846264338327 .....
93  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
94  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
95  *
96  *      Hexadecimal:
97  *              pi = 3.243F6A8885A308D313198A2E....
98  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18    error=.276ulps
99  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
100  *
101  *      In a test run with 1,024,000 random arguments on a VAX, the maximum
102  *      observed errors (compared with the exact trig(x*pi/PI)) were
103  *                      tan(x) : 2.09 ulps (around 4.716340404662354)
104  *                      sin(x) : .861 ulps
105  *                      cos(x) : .857 ulps
106  *
107  * Constants:
108  * The hexadecimal values are the intended ones for the following constants.
109  * The decimal values may be used, provided that the compiler will convert
110  * from decimal to binary accurately enough to produce the hexadecimal values
111  * shown.
112  */
113 
114 #ifdef VAX
115 /*thresh =  2.6117239648121182150E-1    , Hex  2^ -1   *  .85B8636B026EA0 */
116 /*PIo4   =  7.8539816339744830676E-1    , Hex  2^  0   *  .C90FDAA22168C2 */
117 /*PIo2   =  1.5707963267948966135E0     , Hex  2^  1   *  .C90FDAA22168C2 */
118 /*PI3o4  =  2.3561944901923449203E0     , Hex  2^  2   *  .96CBE3F9990E92 */
119 /*PI     =  3.1415926535897932270E0     , Hex  2^  2   *  .C90FDAA22168C2 */
120 /*PI2    =  6.2831853071795864540E0     ; Hex  2^  3   *  .C90FDAA22168C2 */
121 static long    threshx[] = { 0xb8633f85, 0x6ea06b02};
122 #define   thresh    (*(double*)threshx)
123 static long      PIo4x[] = { 0x0fda4049, 0x68c2a221};
124 #define     PIo4    (*(double*)PIo4x)
125 static long      PIo2x[] = { 0x0fda40c9, 0x68c2a221};
126 #define     PIo2    (*(double*)PIo2x)
127 static long      PI3o4x[] = { 0xcbe34116, 0x0e92f999};
128 #define     PI3o4    (*(double*)PI3o4x)
129 static long        PIx[] = { 0x0fda4149, 0x68c2a221};
130 #define       PI    (*(double*)PIx)
131 static long       PI2x[] = { 0x0fda41c9, 0x68c2a221};
132 #define      PI2    (*(double*)PI2x)
133 #else   /* IEEE double  */
134 static double
135 thresh =  2.6117239648121182150E-1    , /*Hex  2^ -2   *  1.0B70C6D604DD4 */
136 PIo4   =  7.8539816339744827900E-1    , /*Hex  2^ -1   *  1.921FB54442D18 */
137 PIo2   =  1.5707963267948965580E0     , /*Hex  2^  0   *  1.921FB54442D18 */
138 PI3o4  =  2.3561944901923448370E0     , /*Hex  2^  1   *  1.2D97C7F3321D2 */
139 PI     =  3.1415926535897931160E0     , /*Hex  2^  1   *  1.921FB54442D18 */
140 PI2    =  6.2831853071795862320E0     ; /*Hex  2^  2   *  1.921FB54442D18 */
141 #endif
142 static double zero=0, one=1, negone= -1, half=1.0/2.0,
143 	      small=1E-10, /* 1+small**2==1; better values for small:
144 					small = 1.5E-9 for VAX D
145 					      = 1.2E-8 for IEEE Double
146 					      = 2.8E-10 for IEEE Extended */
147 	      big=1E20;    /* big = 1/(small**2) */
148 
tan(x)149 double tan(x)
150 double x;
151 {
152         double copysign(),drem(),cos__C(),sin__S(),a,z,ss,cc,c;
153         int finite(),k;
154 
155         /* tan(NaN) and tan(INF) must be NaN */
156             if(!finite(x))  return(x-x);
157         x=drem(x,PI);        /* reduce x into [-PI/2, PI/2] */
158         a=copysign(x,one);   /* ... = abs(x) */
159 	if ( a >= PIo4 ) {k=1; x = copysign( PIo2 - a , x ); }
160 	   else { k=0; if(a < small ) { big + a; return(x); }}
161 
162         z  = x*x;
163         cc = cos__C(z);
164         ss = sin__S(z);
165 	z  = z*half ;		/* Next get c = cos(x) accurately */
166 	c  = (z >= thresh )? half-((z-half)-cc) : one-(z-cc);
167 	if (k==0) return ( x + (x*(z-(cc-ss)))/c );  /* sin/cos */
168 	return( c/(x+x*ss) );	/*                  ... cos/sin */
169 
170 
171 }
sin(x)172 double sin(x)
173 double x;
174 {
175         double copysign(),drem(),sin__S(),cos__C(),a,c,z;
176         int finite();
177 
178         /* sin(NaN) and sin(INF) must be NaN */
179             if(!finite(x))  return(x-x);
180 	x=drem(x,PI2);         /*    reduce x into [-PI, PI] */
181         a=copysign(x,one);
182 	if( a >= PIo4 ) {
183 	     if( a >= PI3o4 )   /* 	.. in [3PI/4,  PI ]  */
184 		x=copysign((a=PI-a),x);
185 
186 	     else {	       /* 	.. in [PI/4, 3PI/4]  */
187 		a=PIo2-a;      /* return sign(x)*C(PI/2-|x|) */
188 		z=a*a;
189 		c=cos__C(z);
190 		z=z*half;
191 		a=(z>=thresh)?half-((z-half)-c):one-(z-c);
192 		return(copysign(a,x));
193 		}
194              }
195 
196         /* return S(x) */
197             if( a < small) { big + a; return(x);}
198             return(x+x*sin__S(x*x));
199 }
200 
cos(x)201 double cos(x)
202 double x;
203 {
204         double copysign(),drem(),sin__S(),cos__C(),a,c,z,s=1.0;
205         int finite();
206 
207         /* cos(NaN) and cos(INF) must be NaN */
208             if(!finite(x))  return(x-x);
209 	x=drem(x,PI2);         /*    reduce x into [-PI, PI] */
210         a=copysign(x,one);
211 	if ( a >= PIo4 ) {
212 	     if ( a >= PI3o4 )  /* 	.. in [3PI/4,  PI ]  */
213 		{ a=PI-a; s= negone; }
214 
215 	     else 	       /* 	.. in [PI/4, 3PI/4]  */
216                                /*        return  S(PI/2-|x|) */
217 		{ a=PIo2-a; return(a+a*sin__S(a*a));}
218 	     }
219 
220 
221         /* return s*C(a) */
222             if( a < small) { big + a; return(s);}
223 	    z=a*a;
224 	    c=cos__C(z);
225 	    z=z*half;
226 	    a=(z>=thresh)?half-((z-half)-c):one-(z-c);
227 	    return(copysign(a,s));
228 }
229 
230 
231 /* sin__S(x*x)
232  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
233  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
234  * CODED IN C BY K.C. NG, 1/21/85;
235  * REVISED BY K.C. NG on 8/13/85.
236  *
237  *	    sin(x*k) - x
238  * RETURN  --------------- on [-PI/4,PI/4] , where k=pi/PI, PI is the rounded
239  *	            x
240  * value of pi in machine precision:
241  *
242  *	Decimal:
243  *		pi = 3.141592653589793 23846264338327 .....
244  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
245  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
246  *
247  *	Hexadecimal:
248  *		pi = 3.243F6A8885A308D313198A2E....
249  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
250  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
251  *
252  * Method:
253  *	1. Let z=x*x. Create a polynomial approximation to
254  *	    (sin(k*x)-x)/x  =  z*(S0 + S1*z^1 + ... + S5*z^5).
255  *	Then
256  *      sin__S(x*x) = z*(S0 + S1*z^1 + ... + S5*z^5)
257  *
258  *	The coefficient S's are obtained by a special Remez algorithm.
259  *
260  * Accuracy:
261  *	In the absence of rounding error, the approximation has absolute error
262  *	less than 2**(-61.11) for VAX D FORMAT, 2**(-57.45) for IEEE DOUBLE.
263  *
264  * Constants:
265  * The hexadecimal values are the intended ones for the following constants.
266  * The decimal values may be used, provided that the compiler will convert
267  * from decimal to binary accurately enough to produce the hexadecimal values
268  * shown.
269  *
270  */
271 
272 #ifdef VAX
273 /*S0     = -1.6666666666666646660E-1    , Hex  2^ -2   * -.AAAAAAAAAAAA71 */
274 /*S1     =  8.3333333333297230413E-3    , Hex  2^ -6   *  .8888888888477F */
275 /*S2     = -1.9841269838362403710E-4    , Hex  2^-12   * -.D00D00CF8A1057 */
276 /*S3     =  2.7557318019967078930E-6    , Hex  2^-18   *  .B8EF1CA326BEDC */
277 /*S4     = -2.5051841873876551398E-8    , Hex  2^-25   * -.D73195374CE1D3 */
278 /*S5     =  1.6028995389845827653E-10   , Hex  2^-32   *  .B03D9C6D26CCCC */
279 /*S6     = -6.2723499671769283121E-13   ; Hex  2^-40   * -.B08D0B7561EA82 */
280 static long        S0x[] = { 0xaaaabf2a, 0xaa71aaaa};
281 #define       S0    (*(double*)S0x)
282 static long        S1x[] = { 0x88883d08, 0x477f8888};
283 #define       S1    (*(double*)S1x)
284 static long        S2x[] = { 0x0d00ba50, 0x1057cf8a};
285 #define       S2    (*(double*)S2x)
286 static long        S3x[] = { 0xef1c3738, 0xbedca326};
287 #define       S3    (*(double*)S3x)
288 static long        S4x[] = { 0x3195b3d7, 0xe1d3374c};
289 #define       S4    (*(double*)S4x)
290 static long        S5x[] = { 0x3d9c3030, 0xcccc6d26};
291 #define       S5    (*(double*)S5x)
292 static long        S6x[] = { 0x8d0bac30, 0xea827561};
293 #define       S6    (*(double*)S6x)
294 #else	/* IEEE double  */
295 static double
296 S0     = -1.6666666666666463126E-1    , /*Hex  2^ -3   * -1.555555555550C */
297 S1     =  8.3333333332992771264E-3    , /*Hex  2^ -7   *  1.111111110C461 */
298 S2     = -1.9841269816180999116E-4    , /*Hex  2^-13   * -1.A01A019746345 */
299 S3     =  2.7557309793219876880E-6    , /*Hex  2^-19   *  1.71DE3209CDCD9 */
300 S4     = -2.5050225177523807003E-8    , /*Hex  2^-26   * -1.AE5C0E319A4EF */
301 S5     =  1.5868926979889205164E-10   ; /*Hex  2^-33   *  1.5CF61DF672B13 */
302 #endif
303 
sin__S(z)304 static double sin__S(z)
305 double z;
306 {
307 #ifdef VAX
308 	return(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*(S5+z*S6)))))));
309 #else 	/* IEEE double */
310 	return(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*S5))))));
311 #endif
312 }
313 
314 
315 /* cos__C(x*x)
316  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
317  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
318  * CODED IN C BY K.C. NG, 1/21/85;
319  * REVISED BY K.C. NG on 8/13/85.
320  *
321  *	   		    x*x
322  * RETURN   cos(k*x) - 1 + ----- on [-PI/4,PI/4],  where k = pi/PI,
323  *	  		     2
324  * PI is the rounded value of pi in machine precision :
325  *
326  *	Decimal:
327  *		pi = 3.141592653589793 23846264338327 .....
328  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
329  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
330  *
331  *	Hexadecimal:
332  *		pi = 3.243F6A8885A308D313198A2E....
333  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
334  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
335  *
336  *
337  * Method:
338  *	1. Let z=x*x. Create a polynomial approximation to
339  *	    cos(k*x)-1+z/2  =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
340  *	then
341  *      cos__C(z) =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
342  *
343  *	The coefficient C's are obtained by a special Remez algorithm.
344  *
345  * Accuracy:
346  *	In the absence of rounding error, the approximation has absolute error
347  *	less than 2**(-64) for VAX D FORMAT, 2**(-58.3) for IEEE DOUBLE.
348  *
349  *
350  * Constants:
351  * The hexadecimal values are the intended ones for the following constants.
352  * The decimal values may be used, provided that the compiler will convert
353  * from decimal to binary accurately enough to produce the hexadecimal values
354  * shown.
355  *
356  */
357 
358 #ifdef VAX
359 /*C0     =  4.1666666666666504759E-2    , Hex  2^ -4   *  .AAAAAAAAAAA9F0 */
360 /*C1     = -1.3888888888865302059E-3    , Hex  2^ -9   * -.B60B60B60A0CCA */
361 /*C2     =  2.4801587285601038265E-5    , Hex  2^-15   *  .D00D00CDCD098F */
362 /*C3     = -2.7557313470902390219E-7    , Hex  2^-21   * -.93F27BB593E805 */
363 /*C4     =  2.0875623401082232009E-9    , Hex  2^-28   *  .8F74C8FA1E3FF0 */
364 /*C5     = -1.1355178117642986178E-11   ; Hex  2^-36   * -.C7C32D0A5C5A63 */
365 static long        C0x[] = { 0xaaaa3e2a, 0xa9f0aaaa};
366 #define       C0    (*(double*)C0x)
367 static long        C1x[] = { 0x0b60bbb6, 0x0ccab60a};
368 #define       C1    (*(double*)C1x)
369 static long        C2x[] = { 0x0d0038d0, 0x098fcdcd};
370 #define       C2    (*(double*)C2x)
371 static long        C3x[] = { 0xf27bb593, 0xe805b593};
372 #define       C3    (*(double*)C3x)
373 static long        C4x[] = { 0x74c8320f, 0x3ff0fa1e};
374 #define       C4    (*(double*)C4x)
375 static long        C5x[] = { 0xc32dae47, 0x5a630a5c};
376 #define       C5    (*(double*)C5x)
377 #else	/* IEEE double  */
378 static double
379 C0     =  4.1666666666666504759E-2    , /*Hex  2^ -5   *  1.555555555553E */
380 C1     = -1.3888888888865301516E-3    , /*Hex  2^-10   * -1.6C16C16C14199 */
381 C2     =  2.4801587269650015769E-5    , /*Hex  2^-16   *  1.A01A01971CAEB */
382 C3     = -2.7557304623183959811E-7    , /*Hex  2^-22   * -1.27E4F1314AD1A */
383 C4     =  2.0873958177697780076E-9    , /*Hex  2^-29   *  1.1EE3B60DDDC8C */
384 C5     = -1.1250289076471311557E-11   ; /*Hex  2^-37   * -1.8BD5986B2A52E */
385 #endif
386 
cos__C(z)387 static double cos__C(z)
388 double z;
389 {
390 	return(z*z*(C0+z*(C1+z*(C2+z*(C3+z*(C4+z*C5))))));
391 }
392