xref: /original-bsd/lib/libm/common/trig.h (revision c3e32dec)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)trig.h	8.1 (Berkeley) 06/04/93
8  */
9 
10 #include "mathimpl.h"
11 
12 vc(thresh, 2.6117239648121182150E-1 ,b863,3f85,6ea0,6b02, -1, .85B8636B026EA0)
13 vc(PIo4,   7.8539816339744830676E-1 ,0fda,4049,68c2,a221,  0, .C90FDAA22168C2)
14 vc(PIo2,   1.5707963267948966135E0  ,0fda,40c9,68c2,a221,  1, .C90FDAA22168C2)
15 vc(PI3o4,  2.3561944901923449203E0  ,cbe3,4116,0e92,f999,  2, .96CBE3F9990E92)
16 vc(PI,     3.1415926535897932270E0  ,0fda,4149,68c2,a221,  2, .C90FDAA22168C2)
17 vc(PI2,    6.2831853071795864540E0  ,0fda,41c9,68c2,a221,  3, .C90FDAA22168C2)
18 
19 ic(thresh, 2.6117239648121182150E-1 , -2, 1.0B70C6D604DD4)
20 ic(PIo4,   7.8539816339744827900E-1 , -1, 1.921FB54442D18)
21 ic(PIo2,   1.5707963267948965580E0  ,  0, 1.921FB54442D18)
22 ic(PI3o4,  2.3561944901923448370E0  ,  1, 1.2D97C7F3321D2)
23 ic(PI,     3.1415926535897931160E0  ,  1, 1.921FB54442D18)
24 ic(PI2,    6.2831853071795862320E0  ,  2, 1.921FB54442D18)
25 
26 #ifdef vccast
27 #define	thresh	vccast(thresh)
28 #define	PIo4	vccast(PIo4)
29 #define	PIo2	vccast(PIo2)
30 #define	PI3o4	vccast(PI3o4)
31 #define	PI	vccast(PI)
32 #define	PI2	vccast(PI2)
33 #endif
34 
35 #ifdef national
36 static long fmaxx[]	= { 0xffffffff, 0x7fefffff};
37 #define   fmax    (*(double*)fmaxx)
38 #endif	/* national */
39 
40 static const double
41 	zero = 0,
42 	one = 1,
43 	negone = -1,
44 	half = 1.0/2.0,
45 	small = 1E-10,	/* 1+small**2 == 1; better values for small:
46 			 *		small	= 1.5E-9 for VAX D
47 			 *			= 1.2E-8 for IEEE Double
48 			 *			= 2.8E-10 for IEEE Extended
49 			 */
50 	big = 1E20;	/* big := 1/(small**2) */
51 
52 /* sin__S(x*x) ... re-implemented as a macro
53  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
54  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
55  * CODED IN C BY K.C. NG, 1/21/85;
56  * REVISED BY K.C. NG on 8/13/85.
57  *
58  *	    sin(x*k) - x
59  * RETURN  --------------- on [-PI/4,PI/4] , where k=pi/PI, PI is the rounded
60  *	            x
61  * value of pi in machine precision:
62  *
63  *	Decimal:
64  *		pi = 3.141592653589793 23846264338327 .....
65  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
66  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
67  *
68  *	Hexadecimal:
69  *		pi = 3.243F6A8885A308D313198A2E....
70  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
71  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
72  *
73  * Method:
74  *	1. Let z=x*x. Create a polynomial approximation to
75  *	    (sin(k*x)-x)/x  =  z*(S0 + S1*z^1 + ... + S5*z^5).
76  *	Then
77  *      sin__S(x*x) = z*(S0 + S1*z^1 + ... + S5*z^5)
78  *
79  *	The coefficient S's are obtained by a special Remez algorithm.
80  *
81  * Accuracy:
82  *	In the absence of rounding error, the approximation has absolute error
83  *	less than 2**(-61.11) for VAX D FORMAT, 2**(-57.45) for IEEE DOUBLE.
84  *
85  * Constants:
86  * The hexadecimal values are the intended ones for the following constants.
87  * The decimal values may be used, provided that the compiler will convert
88  * from decimal to binary accurately enough to produce the hexadecimal values
89  * shown.
90  *
91  */
92 
93 vc(S0, -1.6666666666666646660E-1  ,aaaa,bf2a,aa71,aaaa,  -2, -.AAAAAAAAAAAA71)
94 vc(S1,  8.3333333333297230413E-3  ,8888,3d08,477f,8888,  -6,  .8888888888477F)
95 vc(S2, -1.9841269838362403710E-4  ,0d00,ba50,1057,cf8a, -12, -.D00D00CF8A1057)
96 vc(S3,  2.7557318019967078930E-6  ,ef1c,3738,bedc,a326, -18,  .B8EF1CA326BEDC)
97 vc(S4, -2.5051841873876551398E-8  ,3195,b3d7,e1d3,374c, -25, -.D73195374CE1D3)
98 vc(S5,  1.6028995389845827653E-10 ,3d9c,3030,cccc,6d26, -32,  .B03D9C6D26CCCC)
99 vc(S6, -6.2723499671769283121E-13 ,8d0b,ac30,ea82,7561, -40, -.B08D0B7561EA82)
100 
101 ic(S0, -1.6666666666666463126E-1  ,  -3, -1.555555555550C)
102 ic(S1,  8.3333333332992771264E-3  ,  -7,  1.111111110C461)
103 ic(S2, -1.9841269816180999116E-4  , -13, -1.A01A019746345)
104 ic(S3,  2.7557309793219876880E-6  , -19,  1.71DE3209CDCD9)
105 ic(S4, -2.5050225177523807003E-8  , -26, -1.AE5C0E319A4EF)
106 ic(S5,  1.5868926979889205164E-10 , -33,  1.5CF61DF672B13)
107 
108 #ifdef vccast
109 #define	S0	vccast(S0)
110 #define	S1	vccast(S1)
111 #define	S2	vccast(S2)
112 #define	S3	vccast(S3)
113 #define	S4	vccast(S4)
114 #define	S5	vccast(S5)
115 #define	S6	vccast(S6)
116 #endif
117 
118 #if defined(vax)||defined(tahoe)
119 #  define	sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*(S5+z*S6)))))))
120 #else 	/* defined(vax)||defined(tahoe) */
121 #  define	sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*S5))))))
122 #endif 	/* defined(vax)||defined(tahoe) */
123 
124 /* cos__C(x*x) ... re-implemented as a macro
125  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
126  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
127  * CODED IN C BY K.C. NG, 1/21/85;
128  * REVISED BY K.C. NG on 8/13/85.
129  *
130  *	   		    x*x
131  * RETURN   cos(k*x) - 1 + ----- on [-PI/4,PI/4],  where k = pi/PI,
132  *	  		     2
133  * PI is the rounded value of pi in machine precision :
134  *
135  *	Decimal:
136  *		pi = 3.141592653589793 23846264338327 .....
137  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
138  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
139  *
140  *	Hexadecimal:
141  *		pi = 3.243F6A8885A308D313198A2E....
142  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
143  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
144  *
145  *
146  * Method:
147  *	1. Let z=x*x. Create a polynomial approximation to
148  *	    cos(k*x)-1+z/2  =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
149  *	then
150  *      cos__C(z) =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
151  *
152  *	The coefficient C's are obtained by a special Remez algorithm.
153  *
154  * Accuracy:
155  *	In the absence of rounding error, the approximation has absolute error
156  *	less than 2**(-64) for VAX D FORMAT, 2**(-58.3) for IEEE DOUBLE.
157  *
158  *
159  * Constants:
160  * The hexadecimal values are the intended ones for the following constants.
161  * The decimal values may be used, provided that the compiler will convert
162  * from decimal to binary accurately enough to produce the hexadecimal values
163  * shown.
164  */
165 
166 vc(C0,  4.1666666666666504759E-2  ,aaaa,3e2a,a9f0,aaaa,  -4,  .AAAAAAAAAAA9F0)
167 vc(C1, -1.3888888888865302059E-3  ,0b60,bbb6,0cca,b60a,  -9, -.B60B60B60A0CCA)
168 vc(C2,  2.4801587285601038265E-5  ,0d00,38d0,098f,cdcd, -15,  .D00D00CDCD098F)
169 vc(C3, -2.7557313470902390219E-7  ,f27b,b593,e805,b593, -21, -.93F27BB593E805)
170 vc(C4,  2.0875623401082232009E-9  ,74c8,320f,3ff0,fa1e, -28,  .8F74C8FA1E3FF0)
171 vc(C5, -1.1355178117642986178E-11 ,c32d,ae47,5a63,0a5c, -36, -.C7C32D0A5C5A63)
172 
173 ic(C0,  4.1666666666666504759E-2  ,  -5,  1.555555555553E)
174 ic(C1, -1.3888888888865301516E-3  , -10, -1.6C16C16C14199)
175 ic(C2,  2.4801587269650015769E-5  , -16,  1.A01A01971CAEB)
176 ic(C3, -2.7557304623183959811E-7  , -22, -1.27E4F1314AD1A)
177 ic(C4,  2.0873958177697780076E-9  , -29,  1.1EE3B60DDDC8C)
178 ic(C5, -1.1250289076471311557E-11 , -37, -1.8BD5986B2A52E)
179 
180 #ifdef vccast
181 #define	C0	vccast(C0)
182 #define	C1	vccast(C1)
183 #define	C2	vccast(C2)
184 #define	C3	vccast(C3)
185 #define	C4	vccast(C4)
186 #define	C5	vccast(C5)
187 #endif
188 
189 #define cos__C(z)	(z*z*(C0+z*(C1+z*(C2+z*(C3+z*(C4+z*C5))))))
190