124588Szliu /* 224588Szliu * Copyright (c) 1985 Regents of the University of California. 324588Szliu * 424588Szliu * Use and reproduction of this software are granted in accordance with 524588Szliu * the terms and conditions specified in the Berkeley Software License 624588Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724588Szliu * source, and inclusion of this notice) with the additional understanding 824588Szliu * that all recipients should regard themselves as participants in an 924588Szliu * ongoing research project and hence should feel obligated to report 1024588Szliu * their experiences (good or bad) with these elementary function codes, 1124588Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224588Szliu */ 1324588Szliu 1424588Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*31812Szliu "@(#)asinh.c 1.2 (Berkeley) 8/21/85; 1.3 (ucb.elefunt) 07/10/87"; 1724588Szliu #endif not lint 1824588Szliu 1924588Szliu /* ASINH(X) 2024588Szliu * RETURN THE INVERSE HYPERBOLIC SINE OF X 2124588Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2224588Szliu * CODED IN C BY K.C. NG, 2/16/85; 2324588Szliu * REVISED BY K.C. NG on 3/7/85, 3/24/85, 4/16/85. 2424588Szliu * 2524588Szliu * Required system supported functions : 2624588Szliu * copysign(x,y) 2724588Szliu * sqrt(x) 2824588Szliu * 2924588Szliu * Required kernel function: 3024588Szliu * log1p(x) ...return log(1+x) 3124588Szliu * 3224588Szliu * Method : 3324588Szliu * Based on 3424588Szliu * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ] 3524588Szliu * we have 3624588Szliu * asinh(x) := x if 1+x*x=1, 3724588Szliu * := sign(x)*(log1p(x)+ln2)) if sqrt(1+x*x)=x, else 3824588Szliu * := sign(x)*log1p(|x| + |x|/(1/|x| + sqrt(1+(1/|x|)^2)) ) 3924588Szliu * 4024588Szliu * Accuracy: 4124588Szliu * asinh(x) returns the exact inverse hyperbolic sine of x nearly rounded. 4224588Szliu * In a test run with 52,000 random arguments on a VAX, the maximum 4324588Szliu * observed error was 1.58 ulps (units in the last place). 4424588Szliu * 4524588Szliu * Constants: 4624588Szliu * The hexadecimal values are the intended ones for the following constants. 4724588Szliu * The decimal values may be used, provided that the compiler will convert 4824588Szliu * from decimal to binary accurately enough to produce the hexadecimal values 4924588Szliu * shown. 5024588Szliu */ 5124588Szliu 52*31812Szliu #if (defined(VAX)||defined(TAHOE)) /* VAX D format */ 53*31812Szliu #ifdef VAX 54*31812Szliu #define _0x(A,B) 0x/**/A/**/B 55*31812Szliu #else /* VAX */ 56*31812Szliu #define _0x(A,B) 0x/**/B/**/A 57*31812Szliu #endif /* VAX */ 5824588Szliu /* static double */ 5924588Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 6024588Szliu /* ln2lo = 1.6465949582897081279E-12 ; Hex 2^-39 * .E7BCD5E4F1D9CC */ 61*31812Szliu static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 62*31812Szliu static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 6324588Szliu #define ln2hi (*(double*)ln2hix) 6424588Szliu #define ln2lo (*(double*)ln2lox) 6524588Szliu #else /* IEEE double */ 6624588Szliu static double 6724588Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 6824588Szliu ln2lo = 1.9082149292705877000E-10 ; /*Hex 2^-33 * 1.A39EF35793C76 */ 6924588Szliu #endif 7024588Szliu 7124588Szliu double asinh(x) 7224588Szliu double x; 7324588Szliu { 7424588Szliu double copysign(),log1p(),sqrt(),t,s; 7524588Szliu static double small=1.0E-10, /* fl(1+small*small) == 1 */ 7624588Szliu big =1.0E20, /* fl(1+big) == big */ 7724588Szliu one =1.0 ; 7824588Szliu 79*31812Szliu #if (!defined(VAX)&&!defined(TAHOE)) 8024588Szliu if(x!=x) return(x); /* x is NaN */ 8124588Szliu #endif 8224588Szliu if((t=copysign(x,one))>small) 8324588Szliu if(t<big) { 8424588Szliu s=one/t; return(copysign(log1p(t+t/(s+sqrt(one+s*s))),x)); } 8524588Szliu else /* if |x| > big */ 8624588Szliu {s=log1p(t)+ln2lo; return(copysign(s+ln2hi,x));} 8724588Szliu else /* if |x| < small */ 8824588Szliu return(x); 8924588Szliu } 90