1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  * All recipients should regard themselves as participants in an ongoing
13  * research project and hence should feel obligated to report their
14  * experiences (good or bad) with these elementary function codes, using
15  * the sendbug(8) program, to the authors.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)asincos.c	5.2 (Berkeley) 04/29/88";
20 #endif /* not lint */
21 
22 /* ASIN(X)
23  * RETURNS ARC SINE OF X
24  * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
25  * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
26  *
27  * Required system supported functions:
28  *	copysign(x,y)
29  *	sqrt(x)
30  *
31  * Required kernel function:
32  *	atan2(y,x)
33  *
34  * Method :
35  *	asin(x) = atan2(x,sqrt(1-x*x)); for better accuracy, 1-x*x is
36  *		  computed as follows
37  *			1-x*x                     if x <  0.5,
38  *			2*(1-|x|)-(1-|x|)*(1-|x|) if x >= 0.5.
39  *
40  * Special cases:
41  *	if x is NaN, return x itself;
42  *	if |x|>1, return NaN.
43  *
44  * Accuracy:
45  * 1)  If atan2() uses machine PI, then
46  *
47  *	asin(x) returns (PI/pi) * (the exact arc sine of x) nearly rounded;
48  *	and PI is the exact pi rounded to machine precision (see atan2 for
49  *      details):
50  *
51  *	in decimal:
52  *		pi = 3.141592653589793 23846264338327 .....
53  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
54  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
55  *
56  *	in hexadecimal:
57  *		pi = 3.243F6A8885A308D313198A2E....
58  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
59  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
60  *
61  *	In a test run with more than 200,000 random arguments on a VAX, the
62  *	maximum observed error in ulps (units in the last place) was
63  *	2.06 ulps.      (comparing against (PI/pi)*(exact asin(x)));
64  *
65  * 2)  If atan2() uses true pi, then
66  *
67  *	asin(x) returns the exact asin(x) with error below about 2 ulps.
68  *
69  *	In a test run with more than 1,024,000 random arguments on a VAX, the
70  *	maximum observed error in ulps (units in the last place) was
71  *      1.99 ulps.
72  */
73 
74 double asin(x)
75 double x;
76 {
77 	double s,t,copysign(),atan2(),sqrt(),one=1.0;
78 #if !defined(vax)&&!defined(tahoe)
79 	if(x!=x) return(x);	/* x is NaN */
80 #endif	/* !defined(vax)&&!defined(tahoe) */
81 	s=copysign(x,one);
82 	if(s <= 0.5)
83 	    return(atan2(x,sqrt(one-x*x)));
84 	else
85 	    { t=one-s; s=t+t; return(atan2(x,sqrt(s-t*t))); }
86 
87 }
88 
89 /* ACOS(X)
90  * RETURNS ARC COS OF X
91  * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
92  * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
93  *
94  * Required system supported functions:
95  *	copysign(x,y)
96  *	sqrt(x)
97  *
98  * Required kernel function:
99  *	atan2(y,x)
100  *
101  * Method :
102  *			      ________
103  *                           / 1 - x
104  *	acos(x) = 2*atan2(  / -------- , 1 ) .
105  *                        \/   1 + x
106  *
107  * Special cases:
108  *	if x is NaN, return x itself;
109  *	if |x|>1, return NaN.
110  *
111  * Accuracy:
112  * 1)  If atan2() uses machine PI, then
113  *
114  *	acos(x) returns (PI/pi) * (the exact arc cosine of x) nearly rounded;
115  *	and PI is the exact pi rounded to machine precision (see atan2 for
116  *      details):
117  *
118  *	in decimal:
119  *		pi = 3.141592653589793 23846264338327 .....
120  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
121  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
122  *
123  *	in hexadecimal:
124  *		pi = 3.243F6A8885A308D313198A2E....
125  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
126  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
127  *
128  *	In a test run with more than 200,000 random arguments on a VAX, the
129  *	maximum observed error in ulps (units in the last place) was
130  *	2.07 ulps.      (comparing against (PI/pi)*(exact acos(x)));
131  *
132  * 2)  If atan2() uses true pi, then
133  *
134  *	acos(x) returns the exact acos(x) with error below about 2 ulps.
135  *
136  *	In a test run with more than 1,024,000 random arguments on a VAX, the
137  *	maximum observed error in ulps (units in the last place) was
138  *	2.15 ulps.
139  */
140 
141 double acos(x)
142 double x;
143 {
144 	double t,copysign(),atan2(),sqrt(),one=1.0;
145 #if !defined(vax)&&!defined(tahoe)
146 	if(x!=x) return(x);
147 #endif	/* !defined(vax)&&!defined(tahoe) */
148 	if( x != -1.0)
149 	    t=atan2(sqrt((one-x)/(one+x)),one);
150 	else
151 	    t=atan2(one,0.0);	/* t = PI/2 */
152 	return(t+t);
153 }
154