134124Sbostic /*
2*61283Sbostic * Copyright (c) 1985, 1993
3*61283Sbostic * The Regents of the University of California. All rights reserved.
434124Sbostic *
542655Sbostic * %sccs.include.redist.c%
624587Szliu */
724587Szliu
824587Szliu #ifndef lint
9*61283Sbostic static char sccsid[] = "@(#)asincos.c 8.1 (Berkeley) 06/04/93";
1034124Sbostic #endif /* not lint */
1124587Szliu
1224587Szliu /* ASIN(X)
1324587Szliu * RETURNS ARC SINE OF X
1424587Szliu * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
1524587Szliu * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
1624587Szliu *
1724587Szliu * Required system supported functions:
1824587Szliu * copysign(x,y)
1924587Szliu * sqrt(x)
2024587Szliu *
2124587Szliu * Required kernel function:
2224587Szliu * atan2(y,x)
2324587Szliu *
2424587Szliu * Method :
2524587Szliu * asin(x) = atan2(x,sqrt(1-x*x)); for better accuracy, 1-x*x is
2624587Szliu * computed as follows
2724587Szliu * 1-x*x if x < 0.5,
2824587Szliu * 2*(1-|x|)-(1-|x|)*(1-|x|) if x >= 0.5.
2924587Szliu *
3024587Szliu * Special cases:
3124587Szliu * if x is NaN, return x itself;
3224587Szliu * if |x|>1, return NaN.
3324587Szliu *
3424587Szliu * Accuracy:
3524587Szliu * 1) If atan2() uses machine PI, then
3624587Szliu *
3724587Szliu * asin(x) returns (PI/pi) * (the exact arc sine of x) nearly rounded;
3824587Szliu * and PI is the exact pi rounded to machine precision (see atan2 for
3924587Szliu * details):
4024587Szliu *
4124587Szliu * in decimal:
4224587Szliu * pi = 3.141592653589793 23846264338327 .....
4324587Szliu * 53 bits PI = 3.141592653589793 115997963 ..... ,
4424587Szliu * 56 bits PI = 3.141592653589793 227020265 ..... ,
4524587Szliu *
4624587Szliu * in hexadecimal:
4724587Szliu * pi = 3.243F6A8885A308D313198A2E....
4824587Szliu * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps
4924587Szliu * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps
5024587Szliu *
5124587Szliu * In a test run with more than 200,000 random arguments on a VAX, the
5224587Szliu * maximum observed error in ulps (units in the last place) was
5324587Szliu * 2.06 ulps. (comparing against (PI/pi)*(exact asin(x)));
5424587Szliu *
5524587Szliu * 2) If atan2() uses true pi, then
5624587Szliu *
5724587Szliu * asin(x) returns the exact asin(x) with error below about 2 ulps.
5824587Szliu *
5924587Szliu * In a test run with more than 1,024,000 random arguments on a VAX, the
6024587Szliu * maximum observed error in ulps (units in the last place) was
6124587Szliu * 1.99 ulps.
6224587Szliu */
6324587Szliu
asin(x)6424587Szliu double asin(x)
6524587Szliu double x;
6624587Szliu {
6724587Szliu double s,t,copysign(),atan2(),sqrt(),one=1.0;
6831853Szliu #if !defined(vax)&&!defined(tahoe)
6924587Szliu if(x!=x) return(x); /* x is NaN */
7031853Szliu #endif /* !defined(vax)&&!defined(tahoe) */
7124587Szliu s=copysign(x,one);
7224587Szliu if(s <= 0.5)
7324587Szliu return(atan2(x,sqrt(one-x*x)));
7424587Szliu else
7524587Szliu { t=one-s; s=t+t; return(atan2(x,sqrt(s-t*t))); }
7624587Szliu
7724587Szliu }
7824587Szliu
7924587Szliu /* ACOS(X)
8024587Szliu * RETURNS ARC COS OF X
8124587Szliu * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits)
8224587Szliu * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85.
8324587Szliu *
8424587Szliu * Required system supported functions:
8524587Szliu * copysign(x,y)
8624587Szliu * sqrt(x)
8724587Szliu *
8824587Szliu * Required kernel function:
8924587Szliu * atan2(y,x)
9024587Szliu *
9124587Szliu * Method :
9224587Szliu * ________
9324587Szliu * / 1 - x
9424587Szliu * acos(x) = 2*atan2( / -------- , 1 ) .
9524587Szliu * \/ 1 + x
9624587Szliu *
9724587Szliu * Special cases:
9824587Szliu * if x is NaN, return x itself;
9924587Szliu * if |x|>1, return NaN.
10024587Szliu *
10124587Szliu * Accuracy:
10224587Szliu * 1) If atan2() uses machine PI, then
10324587Szliu *
10424587Szliu * acos(x) returns (PI/pi) * (the exact arc cosine of x) nearly rounded;
10524587Szliu * and PI is the exact pi rounded to machine precision (see atan2 for
10624587Szliu * details):
10724587Szliu *
10824587Szliu * in decimal:
10924587Szliu * pi = 3.141592653589793 23846264338327 .....
11024587Szliu * 53 bits PI = 3.141592653589793 115997963 ..... ,
11124587Szliu * 56 bits PI = 3.141592653589793 227020265 ..... ,
11224587Szliu *
11324587Szliu * in hexadecimal:
11424587Szliu * pi = 3.243F6A8885A308D313198A2E....
11524587Szliu * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps
11624587Szliu * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps
11724587Szliu *
11824587Szliu * In a test run with more than 200,000 random arguments on a VAX, the
11924587Szliu * maximum observed error in ulps (units in the last place) was
12024587Szliu * 2.07 ulps. (comparing against (PI/pi)*(exact acos(x)));
12124587Szliu *
12224587Szliu * 2) If atan2() uses true pi, then
12324587Szliu *
12424587Szliu * acos(x) returns the exact acos(x) with error below about 2 ulps.
12524587Szliu *
12624587Szliu * In a test run with more than 1,024,000 random arguments on a VAX, the
12724587Szliu * maximum observed error in ulps (units in the last place) was
12824587Szliu * 2.15 ulps.
12924587Szliu */
13024587Szliu
acos(x)13124587Szliu double acos(x)
13224587Szliu double x;
13324587Szliu {
13424587Szliu double t,copysign(),atan2(),sqrt(),one=1.0;
13531853Szliu #if !defined(vax)&&!defined(tahoe)
13624587Szliu if(x!=x) return(x);
13731853Szliu #endif /* !defined(vax)&&!defined(tahoe) */
13824587Szliu if( x != -1.0)
13924587Szliu t=atan2(sqrt((one-x)/(one+x)),one);
14024587Szliu else
14124587Szliu t=atan2(one,0.0); /* t = PI/2 */
14224587Szliu return(t+t);
14324587Szliu }
144