124587Szliu /*
224587Szliu  * Copyright (c) 1985 Regents of the University of California.
324587Szliu  *
424587Szliu  * Use and reproduction of this software are granted  in  accordance  with
524587Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
624587Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
724587Szliu  * source, and inclusion of this notice) with the additional understanding
824587Szliu  * that  all  recipients  should regard themselves as participants  in  an
924587Szliu  * ongoing  research  project and hence should  feel  obligated  to report
1024587Szliu  * their  experiences (good or bad) with these elementary function  codes,
1124587Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
1224587Szliu  */
1324587Szliu 
1424587Szliu #ifndef lint
1524706Selefunt static char sccsid[] =
16*31853Szliu "@(#)asincos.c	1.1 (Berkeley) 8/21/85; 1.4 (ucb.elefunt) 07/13/87";
17*31853Szliu #endif	/* not lint */
1824587Szliu 
1924587Szliu /* ASIN(X)
2024587Szliu  * RETURNS ARC SINE OF X
2124587Szliu  * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
2224587Szliu  * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
2324587Szliu  *
2424587Szliu  * Required system supported functions:
2524587Szliu  *	copysign(x,y)
2624587Szliu  *	sqrt(x)
2724587Szliu  *
2824587Szliu  * Required kernel function:
2924587Szliu  *	atan2(y,x)
3024587Szliu  *
3124587Szliu  * Method :
3224587Szliu  *	asin(x) = atan2(x,sqrt(1-x*x)); for better accuracy, 1-x*x is
3324587Szliu  *		  computed as follows
3424587Szliu  *			1-x*x                     if x <  0.5,
3524587Szliu  *			2*(1-|x|)-(1-|x|)*(1-|x|) if x >= 0.5.
3624587Szliu  *
3724587Szliu  * Special cases:
3824587Szliu  *	if x is NaN, return x itself;
3924587Szliu  *	if |x|>1, return NaN.
4024587Szliu  *
4124587Szliu  * Accuracy:
4224587Szliu  * 1)  If atan2() uses machine PI, then
4324587Szliu  *
4424587Szliu  *	asin(x) returns (PI/pi) * (the exact arc sine of x) nearly rounded;
4524587Szliu  *	and PI is the exact pi rounded to machine precision (see atan2 for
4624587Szliu  *      details):
4724587Szliu  *
4824587Szliu  *	in decimal:
4924587Szliu  *		pi = 3.141592653589793 23846264338327 .....
5024587Szliu  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
5124587Szliu  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
5224587Szliu  *
5324587Szliu  *	in hexadecimal:
5424587Szliu  *		pi = 3.243F6A8885A308D313198A2E....
5524587Szliu  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
5624587Szliu  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
5724587Szliu  *
5824587Szliu  *	In a test run with more than 200,000 random arguments on a VAX, the
5924587Szliu  *	maximum observed error in ulps (units in the last place) was
6024587Szliu  *	2.06 ulps.      (comparing against (PI/pi)*(exact asin(x)));
6124587Szliu  *
6224587Szliu  * 2)  If atan2() uses true pi, then
6324587Szliu  *
6424587Szliu  *	asin(x) returns the exact asin(x) with error below about 2 ulps.
6524587Szliu  *
6624587Szliu  *	In a test run with more than 1,024,000 random arguments on a VAX, the
6724587Szliu  *	maximum observed error in ulps (units in the last place) was
6824587Szliu  *      1.99 ulps.
6924587Szliu  */
7024587Szliu 
7124587Szliu double asin(x)
7224587Szliu double x;
7324587Szliu {
7424587Szliu 	double s,t,copysign(),atan2(),sqrt(),one=1.0;
75*31853Szliu #if !defined(vax)&&!defined(tahoe)
7624587Szliu 	if(x!=x) return(x);	/* x is NaN */
77*31853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
7824587Szliu 	s=copysign(x,one);
7924587Szliu 	if(s <= 0.5)
8024587Szliu 	    return(atan2(x,sqrt(one-x*x)));
8124587Szliu 	else
8224587Szliu 	    { t=one-s; s=t+t; return(atan2(x,sqrt(s-t*t))); }
8324587Szliu 
8424587Szliu }
8524587Szliu 
8624587Szliu /* ACOS(X)
8724587Szliu  * RETURNS ARC COS OF X
8824587Szliu  * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
8924587Szliu  * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
9024587Szliu  *
9124587Szliu  * Required system supported functions:
9224587Szliu  *	copysign(x,y)
9324587Szliu  *	sqrt(x)
9424587Szliu  *
9524587Szliu  * Required kernel function:
9624587Szliu  *	atan2(y,x)
9724587Szliu  *
9824587Szliu  * Method :
9924587Szliu  *			      ________
10024587Szliu  *                           / 1 - x
10124587Szliu  *	acos(x) = 2*atan2(  / -------- , 1 ) .
10224587Szliu  *                        \/   1 + x
10324587Szliu  *
10424587Szliu  * Special cases:
10524587Szliu  *	if x is NaN, return x itself;
10624587Szliu  *	if |x|>1, return NaN.
10724587Szliu  *
10824587Szliu  * Accuracy:
10924587Szliu  * 1)  If atan2() uses machine PI, then
11024587Szliu  *
11124587Szliu  *	acos(x) returns (PI/pi) * (the exact arc cosine of x) nearly rounded;
11224587Szliu  *	and PI is the exact pi rounded to machine precision (see atan2 for
11324587Szliu  *      details):
11424587Szliu  *
11524587Szliu  *	in decimal:
11624587Szliu  *		pi = 3.141592653589793 23846264338327 .....
11724587Szliu  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
11824587Szliu  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
11924587Szliu  *
12024587Szliu  *	in hexadecimal:
12124587Szliu  *		pi = 3.243F6A8885A308D313198A2E....
12224587Szliu  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
12324587Szliu  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
12424587Szliu  *
12524587Szliu  *	In a test run with more than 200,000 random arguments on a VAX, the
12624587Szliu  *	maximum observed error in ulps (units in the last place) was
12724587Szliu  *	2.07 ulps.      (comparing against (PI/pi)*(exact acos(x)));
12824587Szliu  *
12924587Szliu  * 2)  If atan2() uses true pi, then
13024587Szliu  *
13124587Szliu  *	acos(x) returns the exact acos(x) with error below about 2 ulps.
13224587Szliu  *
13324587Szliu  *	In a test run with more than 1,024,000 random arguments on a VAX, the
13424587Szliu  *	maximum observed error in ulps (units in the last place) was
13524587Szliu  *	2.15 ulps.
13624587Szliu  */
13724587Szliu 
13824587Szliu double acos(x)
13924587Szliu double x;
14024587Szliu {
14124587Szliu 	double t,copysign(),atan2(),sqrt(),one=1.0;
142*31853Szliu #if !defined(vax)&&!defined(tahoe)
14324587Szliu 	if(x!=x) return(x);
144*31853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
14524587Szliu 	if( x != -1.0)
14624587Szliu 	    t=atan2(sqrt((one-x)/(one+x)),one);
14724587Szliu 	else
14824587Szliu 	    t=atan2(one,0.0);	/* t = PI/2 */
14924587Szliu 	return(t+t);
15024587Szliu }
151