124586Szliu /* 224586Szliu * Copyright (c) 1985 Regents of the University of California. 324586Szliu * 424586Szliu * Use and reproduction of this software are granted in accordance with 524586Szliu * the terms and conditions specified in the Berkeley Software License 624586Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724586Szliu * source, and inclusion of this notice) with the additional understanding 824586Szliu * that all recipients should regard themselves as participants in an 924586Szliu * ongoing research project and hence should feel obligated to report 1024586Szliu * their experiences (good or bad) with these elementary function codes, 1124586Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224586Szliu */ 1324586Szliu 1424586Szliu #ifndef lint 15*24706Selefunt static char sccsid[] = 16*24706Selefunt "@(#)acosh.c 1.2 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/11/85"; 1724586Szliu #endif not lint 1824586Szliu 1924586Szliu /* ACOSH(X) 2024586Szliu * RETURN THE INVERSE HYPERBOLIC COSINE OF X 2124586Szliu * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS) 2224586Szliu * CODED IN C BY K.C. NG, 2/16/85; 2324586Szliu * REVISED BY K.C. NG on 3/6/85, 3/24/85, 4/16/85, 8/17/85. 2424586Szliu * 2524586Szliu * Required system supported functions : 2624586Szliu * sqrt(x) 2724586Szliu * 2824586Szliu * Required kernel function: 2924586Szliu * log1p(x) ...return log(1+x) 3024586Szliu * 3124586Szliu * Method : 3224586Szliu * Based on 3324586Szliu * acosh(x) = log [ x + sqrt(x*x-1) ] 3424586Szliu * we have 3524586Szliu * acosh(x) := log1p(x)+ln2, if (x > 1.0E20); else 3624586Szliu * acosh(x) := log1p( sqrt(x-1) * (sqrt(x-1) + sqrt(x+1)) ) . 3724586Szliu * These formulae avoid the over/underflow complication. 3824586Szliu * 3924586Szliu * Special cases: 4024586Szliu * acosh(x) is NaN with signal if x<1. 4124586Szliu * acosh(NaN) is NaN without signal. 4224586Szliu * 4324586Szliu * Accuracy: 4424586Szliu * acosh(x) returns the exact inverse hyperbolic cosine of x nearly 4524586Szliu * rounded. In a test run with 512,000 random arguments on a VAX, the 4624586Szliu * maximum observed error was 3.30 ulps (units of the last place) at 4724586Szliu * x=1.0070493753568216 . 4824586Szliu * 4924586Szliu * Constants: 5024586Szliu * The hexadecimal values are the intended ones for the following constants. 5124586Szliu * The decimal values may be used, provided that the compiler will convert 5224586Szliu * from decimal to binary accurately enough to produce the hexadecimal values 5324586Szliu * shown. 5424586Szliu */ 5524586Szliu 5624586Szliu #ifdef VAX /* VAX D format */ 5724586Szliu /* static double */ 5824586Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 5924586Szliu /* ln2lo = 1.6465949582897081279E-12 ; Hex 2^-39 * .E7BCD5E4F1D9CC */ 6024586Szliu static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 6124586Szliu static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 6224586Szliu #define ln2hi (*(double*)ln2hix) 6324586Szliu #define ln2lo (*(double*)ln2lox) 6424586Szliu #else /* IEEE double */ 6524586Szliu static double 6624586Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 6724586Szliu ln2lo = 1.9082149292705877000E-10 ; /*Hex 2^-33 * 1.A39EF35793C76 */ 6824586Szliu #endif 6924586Szliu 7024586Szliu double acosh(x) 7124586Szliu double x; 7224586Szliu { 7324586Szliu double log1p(),sqrt(),t,big=1.E20; /* big+1==big */ 7424586Szliu 7524586Szliu #ifndef VAX 7624586Szliu if(x!=x) return(x); /* x is NaN */ 7724586Szliu #endif 7824586Szliu 7924586Szliu /* return log1p(x) + log(2) if x is large */ 8024586Szliu if(x>big) {t=log1p(x)+ln2lo; return(t+ln2hi);} 8124586Szliu 8224586Szliu t=sqrt(x-1.0); 8324586Szliu return(log1p(t*(t+sqrt(x+1.0)))); 8424586Szliu } 85