1*34124Sbostic /* 224587Szliu * Copyright (c) 1985 Regents of the University of California. 3*34124Sbostic * All rights reserved. 4*34124Sbostic * 5*34124Sbostic * Redistribution and use in source and binary forms are permitted 6*34124Sbostic * provided that this notice is preserved and that due credit is given 7*34124Sbostic * to the University of California at Berkeley. The name of the University 8*34124Sbostic * may not be used to endorse or promote products derived from this 9*34124Sbostic * software without specific prior written permission. This software 10*34124Sbostic * is provided ``as is'' without express or implied warranty. 11*34124Sbostic * 12*34124Sbostic * All recipients should regard themselves as participants in an ongoing 13*34124Sbostic * research project and hence should feel obligated to report their 14*34124Sbostic * experiences (good or bad) with these elementary function codes, using 15*34124Sbostic * the sendbug(8) program, to the authors. 1624587Szliu */ 1724587Szliu 1824587Szliu #ifndef lint 19*34124Sbostic static char sccsid[] = "@(#)asincos.c 5.2 (Berkeley) 04/29/88"; 20*34124Sbostic #endif /* not lint */ 2124587Szliu 2224587Szliu /* ASIN(X) 2324587Szliu * RETURNS ARC SINE OF X 2424587Szliu * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits) 2524587Szliu * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85. 2624587Szliu * 2724587Szliu * Required system supported functions: 2824587Szliu * copysign(x,y) 2924587Szliu * sqrt(x) 3024587Szliu * 3124587Szliu * Required kernel function: 3224587Szliu * atan2(y,x) 3324587Szliu * 3424587Szliu * Method : 3524587Szliu * asin(x) = atan2(x,sqrt(1-x*x)); for better accuracy, 1-x*x is 3624587Szliu * computed as follows 3724587Szliu * 1-x*x if x < 0.5, 3824587Szliu * 2*(1-|x|)-(1-|x|)*(1-|x|) if x >= 0.5. 3924587Szliu * 4024587Szliu * Special cases: 4124587Szliu * if x is NaN, return x itself; 4224587Szliu * if |x|>1, return NaN. 4324587Szliu * 4424587Szliu * Accuracy: 4524587Szliu * 1) If atan2() uses machine PI, then 4624587Szliu * 4724587Szliu * asin(x) returns (PI/pi) * (the exact arc sine of x) nearly rounded; 4824587Szliu * and PI is the exact pi rounded to machine precision (see atan2 for 4924587Szliu * details): 5024587Szliu * 5124587Szliu * in decimal: 5224587Szliu * pi = 3.141592653589793 23846264338327 ..... 5324587Szliu * 53 bits PI = 3.141592653589793 115997963 ..... , 5424587Szliu * 56 bits PI = 3.141592653589793 227020265 ..... , 5524587Szliu * 5624587Szliu * in hexadecimal: 5724587Szliu * pi = 3.243F6A8885A308D313198A2E.... 5824587Szliu * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps 5924587Szliu * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps 6024587Szliu * 6124587Szliu * In a test run with more than 200,000 random arguments on a VAX, the 6224587Szliu * maximum observed error in ulps (units in the last place) was 6324587Szliu * 2.06 ulps. (comparing against (PI/pi)*(exact asin(x))); 6424587Szliu * 6524587Szliu * 2) If atan2() uses true pi, then 6624587Szliu * 6724587Szliu * asin(x) returns the exact asin(x) with error below about 2 ulps. 6824587Szliu * 6924587Szliu * In a test run with more than 1,024,000 random arguments on a VAX, the 7024587Szliu * maximum observed error in ulps (units in the last place) was 7124587Szliu * 1.99 ulps. 7224587Szliu */ 7324587Szliu 7424587Szliu double asin(x) 7524587Szliu double x; 7624587Szliu { 7724587Szliu double s,t,copysign(),atan2(),sqrt(),one=1.0; 7831853Szliu #if !defined(vax)&&!defined(tahoe) 7924587Szliu if(x!=x) return(x); /* x is NaN */ 8031853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 8124587Szliu s=copysign(x,one); 8224587Szliu if(s <= 0.5) 8324587Szliu return(atan2(x,sqrt(one-x*x))); 8424587Szliu else 8524587Szliu { t=one-s; s=t+t; return(atan2(x,sqrt(s-t*t))); } 8624587Szliu 8724587Szliu } 8824587Szliu 8924587Szliu /* ACOS(X) 9024587Szliu * RETURNS ARC COS OF X 9124587Szliu * DOUBLE PRECISION (IEEE DOUBLE 53 bits, VAX D FORMAT 56 bits) 9224587Szliu * CODED IN C BY K.C. NG, 4/16/85, REVISED ON 6/10/85. 9324587Szliu * 9424587Szliu * Required system supported functions: 9524587Szliu * copysign(x,y) 9624587Szliu * sqrt(x) 9724587Szliu * 9824587Szliu * Required kernel function: 9924587Szliu * atan2(y,x) 10024587Szliu * 10124587Szliu * Method : 10224587Szliu * ________ 10324587Szliu * / 1 - x 10424587Szliu * acos(x) = 2*atan2( / -------- , 1 ) . 10524587Szliu * \/ 1 + x 10624587Szliu * 10724587Szliu * Special cases: 10824587Szliu * if x is NaN, return x itself; 10924587Szliu * if |x|>1, return NaN. 11024587Szliu * 11124587Szliu * Accuracy: 11224587Szliu * 1) If atan2() uses machine PI, then 11324587Szliu * 11424587Szliu * acos(x) returns (PI/pi) * (the exact arc cosine of x) nearly rounded; 11524587Szliu * and PI is the exact pi rounded to machine precision (see atan2 for 11624587Szliu * details): 11724587Szliu * 11824587Szliu * in decimal: 11924587Szliu * pi = 3.141592653589793 23846264338327 ..... 12024587Szliu * 53 bits PI = 3.141592653589793 115997963 ..... , 12124587Szliu * 56 bits PI = 3.141592653589793 227020265 ..... , 12224587Szliu * 12324587Szliu * in hexadecimal: 12424587Szliu * pi = 3.243F6A8885A308D313198A2E.... 12524587Szliu * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps 12624587Szliu * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps 12724587Szliu * 12824587Szliu * In a test run with more than 200,000 random arguments on a VAX, the 12924587Szliu * maximum observed error in ulps (units in the last place) was 13024587Szliu * 2.07 ulps. (comparing against (PI/pi)*(exact acos(x))); 13124587Szliu * 13224587Szliu * 2) If atan2() uses true pi, then 13324587Szliu * 13424587Szliu * acos(x) returns the exact acos(x) with error below about 2 ulps. 13524587Szliu * 13624587Szliu * In a test run with more than 1,024,000 random arguments on a VAX, the 13724587Szliu * maximum observed error in ulps (units in the last place) was 13824587Szliu * 2.15 ulps. 13924587Szliu */ 14024587Szliu 14124587Szliu double acos(x) 14224587Szliu double x; 14324587Szliu { 14424587Szliu double t,copysign(),atan2(),sqrt(),one=1.0; 14531853Szliu #if !defined(vax)&&!defined(tahoe) 14624587Szliu if(x!=x) return(x); 14731853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 14824587Szliu if( x != -1.0) 14924587Szliu t=atan2(sqrt((one-x)/(one+x)),one); 15024587Szliu else 15124587Szliu t=atan2(one,0.0); /* t = PI/2 */ 15224587Szliu return(t+t); 15324587Szliu } 154