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