xref: /csrg-svn/lib/libm/common/trig.h (revision 35680)
134127Sbostic /*
231931Szliu  * Copyright (c) 1987 Regents of the University of California.
334127Sbostic  * All rights reserved.
434127Sbostic  *
534127Sbostic  * Redistribution and use in source and binary forms are permitted
634928Sbostic  * provided that the above copyright notice and this paragraph are
734928Sbostic  * duplicated in all such forms and that any documentation,
834928Sbostic  * advertising materials, and other materials related to such
934928Sbostic  * distribution and use acknowledge that the software was developed
1034928Sbostic  * by the University of California, Berkeley.  The name of the
1134928Sbostic  * University may not be used to endorse or promote products derived
1234928Sbostic  * from this software without specific prior written permission.
1334928Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434928Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534928Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634127Sbostic  *
1734127Sbostic  * All recipients should regard themselves as participants in an ongoing
1834127Sbostic  * research project and hence should feel obligated to report their
1934127Sbostic  * experiences (good or bad) with these elementary function codes, using
2034127Sbostic  * the sendbug(8) program, to the authors.
2134127Sbostic  *
22*35680Sbostic  *	@(#)trig.h	5.4 (Berkeley) 09/22/88
2331931Szliu  */
2434127Sbostic 
25*35680Sbostic #include "mathimpl.h"
26*35680Sbostic 
27*35680Sbostic vc(thresh, 2.6117239648121182150E-1 ,b863,3f85,6ea0,6b02, -1, .85B8636B026EA0)
28*35680Sbostic vc(PIo4,   7.8539816339744830676E-1 ,0fda,4049,68c2,a221,  0, .C90FDAA22168C2)
29*35680Sbostic vc(PIo2,   1.5707963267948966135E0  ,0fda,40c9,68c2,a221,  1, .C90FDAA22168C2)
30*35680Sbostic vc(PI3o4,  2.3561944901923449203E0  ,cbe3,4116,0e92,f999,  2, .96CBE3F9990E92)
31*35680Sbostic vc(PI,     3.1415926535897932270E0  ,0fda,4149,68c2,a221,  2, .C90FDAA22168C2)
32*35680Sbostic vc(PI2,    6.2831853071795864540E0  ,0fda,41c9,68c2,a221,  3, .C90FDAA22168C2)
33*35680Sbostic 
34*35680Sbostic ic(thresh, 2.6117239648121182150E-1 , -2, 1.0B70C6D604DD4)
35*35680Sbostic ic(PIo4,   7.8539816339744827900E-1 , -1, 1.921FB54442D18)
36*35680Sbostic ic(PIo2,   1.5707963267948965580E0  ,  0, 1.921FB54442D18)
37*35680Sbostic ic(PI3o4,  2.3561944901923448370E0  ,  1, 1.2D97C7F3321D2)
38*35680Sbostic ic(PI,     3.1415926535897931160E0  ,  1, 1.921FB54442D18)
39*35680Sbostic ic(PI2,    6.2831853071795862320E0  ,  2, 1.921FB54442D18)
40*35680Sbostic 
41*35680Sbostic #ifdef vccast
42*35680Sbostic #define	thresh	vccast(thresh)
43*35680Sbostic #define	PIo4	vccast(PIo4)
44*35680Sbostic #define	PIo2	vccast(PIo2)
45*35680Sbostic #define	PI3o4	vccast(PI3o4)
46*35680Sbostic #define	PI	vccast(PI)
47*35680Sbostic #define	PI2	vccast(PI2)
48*35680Sbostic #endif
49*35680Sbostic 
5031931Szliu #ifdef national
5131931Szliu static long fmaxx[]	= { 0xffffffff, 0x7fefffff};
5231931Szliu #define   fmax    (*(double*)fmaxx)
5331931Szliu #endif	/* national */
54*35680Sbostic 
55*35680Sbostic static const double
5631931Szliu 	zero = 0,
5731931Szliu 	one = 1,
5831931Szliu 	negone = -1,
5931931Szliu 	half = 1.0/2.0,
6031931Szliu 	small = 1E-10,	/* 1+small**2 == 1; better values for small:
6131931Szliu 			 *		small	= 1.5E-9 for VAX D
6231931Szliu 			 *			= 1.2E-8 for IEEE Double
6331931Szliu 			 *			= 2.8E-10 for IEEE Extended
6431931Szliu 			 */
6531931Szliu 	big = 1E20;	/* big := 1/(small**2) */
6631931Szliu 
6731931Szliu /* sin__S(x*x) ... re-implemented as a macro
6831931Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
6931931Szliu  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
7031931Szliu  * CODED IN C BY K.C. NG, 1/21/85;
7131931Szliu  * REVISED BY K.C. NG on 8/13/85.
7231931Szliu  *
7331931Szliu  *	    sin(x*k) - x
7431931Szliu  * RETURN  --------------- on [-PI/4,PI/4] , where k=pi/PI, PI is the rounded
7531931Szliu  *	            x
7631931Szliu  * value of pi in machine precision:
7731931Szliu  *
7831931Szliu  *	Decimal:
7931931Szliu  *		pi = 3.141592653589793 23846264338327 .....
8031931Szliu  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
8131931Szliu  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
8231931Szliu  *
8331931Szliu  *	Hexadecimal:
8431931Szliu  *		pi = 3.243F6A8885A308D313198A2E....
8531931Szliu  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
8631931Szliu  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
8731931Szliu  *
8831931Szliu  * Method:
8931931Szliu  *	1. Let z=x*x. Create a polynomial approximation to
9031931Szliu  *	    (sin(k*x)-x)/x  =  z*(S0 + S1*z^1 + ... + S5*z^5).
9131931Szliu  *	Then
9231931Szliu  *      sin__S(x*x) = z*(S0 + S1*z^1 + ... + S5*z^5)
9331931Szliu  *
9431931Szliu  *	The coefficient S's are obtained by a special Remez algorithm.
9531931Szliu  *
9631931Szliu  * Accuracy:
9731931Szliu  *	In the absence of rounding error, the approximation has absolute error
9831931Szliu  *	less than 2**(-61.11) for VAX D FORMAT, 2**(-57.45) for IEEE DOUBLE.
9931931Szliu  *
10031931Szliu  * Constants:
10131931Szliu  * The hexadecimal values are the intended ones for the following constants.
10231931Szliu  * The decimal values may be used, provided that the compiler will convert
10331931Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
10431931Szliu  * shown.
10531931Szliu  *
10631931Szliu  */
10731931Szliu 
108*35680Sbostic vc(S0, -1.6666666666666646660E-1  ,aaaa,bf2a,aa71,aaaa,  -2, -.AAAAAAAAAAAA71)
109*35680Sbostic vc(S1,  8.3333333333297230413E-3  ,8888,3d08,477f,8888,  -6,  .8888888888477F)
110*35680Sbostic vc(S2, -1.9841269838362403710E-4  ,0d00,ba50,1057,cf8a, -12, -.D00D00CF8A1057)
111*35680Sbostic vc(S3,  2.7557318019967078930E-6  ,ef1c,3738,bedc,a326, -18,  .B8EF1CA326BEDC)
112*35680Sbostic vc(S4, -2.5051841873876551398E-8  ,3195,b3d7,e1d3,374c, -25, -.D73195374CE1D3)
113*35680Sbostic vc(S5,  1.6028995389845827653E-10 ,3d9c,3030,cccc,6d26, -32,  .B03D9C6D26CCCC)
114*35680Sbostic vc(S6, -6.2723499671769283121E-13 ,8d0b,ac30,ea82,7561, -40, -.B08D0B7561EA82)
115*35680Sbostic 
116*35680Sbostic ic(S0, -1.6666666666666463126E-1  ,  -3, -1.555555555550C)
117*35680Sbostic ic(S1,  8.3333333332992771264E-3  ,  -7,  1.111111110C461)
118*35680Sbostic ic(S2, -1.9841269816180999116E-4  , -13, -1.A01A019746345)
119*35680Sbostic ic(S3,  2.7557309793219876880E-6  , -19,  1.71DE3209CDCD9)
120*35680Sbostic ic(S4, -2.5050225177523807003E-8  , -26, -1.AE5C0E319A4EF)
121*35680Sbostic ic(S5,  1.5868926979889205164E-10 , -33,  1.5CF61DF672B13)
122*35680Sbostic 
123*35680Sbostic #ifdef vccast
124*35680Sbostic #define	S0	vccast(S0)
125*35680Sbostic #define	S1	vccast(S1)
126*35680Sbostic #define	S2	vccast(S2)
127*35680Sbostic #define	S3	vccast(S3)
128*35680Sbostic #define	S4	vccast(S4)
129*35680Sbostic #define	S5	vccast(S5)
130*35680Sbostic #define	S6	vccast(S6)
13131931Szliu #endif
13231931Szliu 
13331931Szliu #if defined(vax)||defined(tahoe)
134*35680Sbostic #  define	sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*(S5+z*S6)))))))
13531931Szliu #else 	/* defined(vax)||defined(tahoe) */
136*35680Sbostic #  define	sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*S5))))))
13731931Szliu #endif 	/* defined(vax)||defined(tahoe) */
13831931Szliu 
13931931Szliu /* cos__C(x*x) ... re-implemented as a macro
14031931Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
14131931Szliu  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
14231931Szliu  * CODED IN C BY K.C. NG, 1/21/85;
14331931Szliu  * REVISED BY K.C. NG on 8/13/85.
14431931Szliu  *
14531931Szliu  *	   		    x*x
14631931Szliu  * RETURN   cos(k*x) - 1 + ----- on [-PI/4,PI/4],  where k = pi/PI,
14731931Szliu  *	  		     2
14831931Szliu  * PI is the rounded value of pi in machine precision :
14931931Szliu  *
15031931Szliu  *	Decimal:
15131931Szliu  *		pi = 3.141592653589793 23846264338327 .....
15231931Szliu  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
15331931Szliu  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
15431931Szliu  *
15531931Szliu  *	Hexadecimal:
15631931Szliu  *		pi = 3.243F6A8885A308D313198A2E....
15731931Szliu  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
15831931Szliu  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
15931931Szliu  *
16031931Szliu  *
16131931Szliu  * Method:
16231931Szliu  *	1. Let z=x*x. Create a polynomial approximation to
16331931Szliu  *	    cos(k*x)-1+z/2  =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
16431931Szliu  *	then
16531931Szliu  *      cos__C(z) =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
16631931Szliu  *
16731931Szliu  *	The coefficient C's are obtained by a special Remez algorithm.
16831931Szliu  *
16931931Szliu  * Accuracy:
17031931Szliu  *	In the absence of rounding error, the approximation has absolute error
17131931Szliu  *	less than 2**(-64) for VAX D FORMAT, 2**(-58.3) for IEEE DOUBLE.
17231931Szliu  *
17331931Szliu  *
17431931Szliu  * Constants:
17531931Szliu  * The hexadecimal values are the intended ones for the following constants.
17631931Szliu  * The decimal values may be used, provided that the compiler will convert
17731931Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
17831931Szliu  * shown.
17931931Szliu  */
18031931Szliu 
181*35680Sbostic vc(C0,  4.1666666666666504759E-2  ,aaaa,3e2a,a9f0,aaaa,  -4,  .AAAAAAAAAAA9F0)
182*35680Sbostic vc(C1, -1.3888888888865302059E-3  ,0b60,bbb6,0cca,b60a,  -9, -.B60B60B60A0CCA)
183*35680Sbostic vc(C2,  2.4801587285601038265E-5  ,0d00,38d0,098f,cdcd, -15,  .D00D00CDCD098F)
184*35680Sbostic vc(C3, -2.7557313470902390219E-7  ,f27b,b593,e805,b593, -21, -.93F27BB593E805)
185*35680Sbostic vc(C4,  2.0875623401082232009E-9  ,74c8,320f,3ff0,fa1e, -28,  .8F74C8FA1E3FF0)
186*35680Sbostic vc(C5, -1.1355178117642986178E-11 ,c32d,ae47,5a63,0a5c, -36, -.C7C32D0A5C5A63)
18731931Szliu 
188*35680Sbostic ic(C0,  4.1666666666666504759E-2  ,  -5,  1.555555555553E)
189*35680Sbostic ic(C1, -1.3888888888865301516E-3  , -10, -1.6C16C16C14199)
190*35680Sbostic ic(C2,  2.4801587269650015769E-5  , -16,  1.A01A01971CAEB)
191*35680Sbostic ic(C3, -2.7557304623183959811E-7  , -22, -1.27E4F1314AD1A)
192*35680Sbostic ic(C4,  2.0873958177697780076E-9  , -29,  1.1EE3B60DDDC8C)
193*35680Sbostic ic(C5, -1.1250289076471311557E-11 , -37, -1.8BD5986B2A52E)
194*35680Sbostic 
195*35680Sbostic #ifdef vccast
196*35680Sbostic #define	C0	vccast(C0)
197*35680Sbostic #define	C1	vccast(C1)
198*35680Sbostic #define	C2	vccast(C2)
199*35680Sbostic #define	C3	vccast(C3)
200*35680Sbostic #define	C4	vccast(C4)
201*35680Sbostic #define	C5	vccast(C5)
202*35680Sbostic #endif
203*35680Sbostic 
20431931Szliu #define cos__C(z)	(z*z*(C0+z*(C1+z*(C2+z*(C3+z*(C4+z*C5))))))
205