124591Szliu /* 224591Szliu * Copyright (c) 1985 Regents of the University of California. 324591Szliu * 424591Szliu * Use and reproduction of this software are granted in accordance with 524591Szliu * the terms and conditions specified in the Berkeley Software License 624591Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724591Szliu * source, and inclusion of this notice) with the additional understanding 824591Szliu * that all recipients should regard themselves as participants in an 924591Szliu * ongoing research project and hence should feel obligated to report 1024591Szliu * their experiences (good or bad) with these elementary function codes, 1124591Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224591Szliu */ 1324591Szliu 1424591Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*26893Selefunt "@(#)cosh.c 1.2 (Berkeley) 8/21/85; 1.3 (ucb.elefunt) 03/16/86"; 1724591Szliu #endif not lint 1824591Szliu 1924591Szliu /* COSH(X) 2024591Szliu * RETURN THE HYPERBOLIC COSINE OF X 2124591Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2224591Szliu * CODED IN C BY K.C. NG, 1/8/85; 2324591Szliu * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85. 2424591Szliu * 2524591Szliu * Required system supported functions : 2624591Szliu * copysign(x,y) 2724591Szliu * scalb(x,N) 2824591Szliu * 2924591Szliu * Required kernel function: 3024591Szliu * exp(x) 3124591Szliu * exp__E(x,c) ...return exp(x+c)-1-x for |x|<0.3465 3224591Szliu * 3324591Szliu * Method : 3424591Szliu * 1. Replace x by |x|. 3524591Szliu * 2. 3624591Szliu * [ exp(x) - 1 ]^2 3724591Szliu * 0 <= x <= 0.3465 : cosh(x) := 1 + ------------------- 3824591Szliu * 2*exp(x) 3924591Szliu * 4024591Szliu * exp(x) + 1/exp(x) 4124591Szliu * 0.3465 <= x <= 22 : cosh(x) := ------------------- 4224591Szliu * 2 4324591Szliu * 22 <= x <= lnovfl : cosh(x) := exp(x)/2 4424591Szliu * lnovfl <= x <= lnovfl+log(2) 4524591Szliu * : cosh(x) := exp(x)/2 (avoid overflow) 4624591Szliu * log(2)+lnovfl < x < INF: overflow to INF 4724591Szliu * 4824591Szliu * Note: .3465 is a number near one half of ln2. 4924591Szliu * 5024591Szliu * Special cases: 5124591Szliu * cosh(x) is x if x is +INF, -INF, or NaN. 5224591Szliu * only cosh(0)=1 is exact for finite x. 5324591Szliu * 5424591Szliu * Accuracy: 5524591Szliu * cosh(x) returns the exact hyperbolic cosine of x nearly rounded. 5624591Szliu * In a test run with 768,000 random arguments on a VAX, the maximum 5724591Szliu * observed error was 1.23 ulps (units in the last place). 5824591Szliu * 5924591Szliu * Constants: 6024591Szliu * The hexadecimal values are the intended ones for the following constants. 6124591Szliu * The decimal values may be used, provided that the compiler will convert 6224591Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6324591Szliu * shown. 6424591Szliu */ 6524591Szliu 6624591Szliu #ifdef VAX 67*26893Selefunt /* static double */ 6824591Szliu /* mln2hi = 8.8029691931113054792E1 , Hex 2^ 7 * .B00F33C7E22BDB */ 6924591Szliu /* mln2lo = -4.9650192275318476525E-16 , Hex 2^-50 * -.8F1B60279E582A */ 7024591Szliu /* lnovfl = 8.8029691931113053016E1 ; Hex 2^ 7 * .B00F33C7E22BDA */ 7124591Szliu static long mln2hix[] = { 0x0f3343b0, 0x2bdbc7e2}; 7224591Szliu static long mln2lox[] = { 0x1b60a70f, 0x582a279e}; 7324591Szliu static long lnovflx[] = { 0x0f3343b0, 0x2bdac7e2}; 7424591Szliu #define mln2hi (*(double*)mln2hix) 7524591Szliu #define mln2lo (*(double*)mln2lox) 7624591Szliu #define lnovfl (*(double*)lnovflx) 7724591Szliu #else /* IEEE double */ 78*26893Selefunt static double 7924591Szliu mln2hi = 7.0978271289338397310E2 , /*Hex 2^ 10 * 1.62E42FEFA39EF */ 8024591Szliu mln2lo = 2.3747039373786107478E-14 , /*Hex 2^-45 * 1.ABC9E3B39803F */ 8124591Szliu lnovfl = 7.0978271289338397310E2 ; /*Hex 2^ 9 * 1.62E42FEFA39EF */ 8224591Szliu #endif 8324591Szliu 8424591Szliu #ifdef VAX 8524591Szliu static max = 126 ; 8624591Szliu #else /* IEEE double */ 8724591Szliu static max = 1023 ; 8824591Szliu #endif 8924591Szliu 9024591Szliu double cosh(x) 9124591Szliu double x; 9224591Szliu { 9324591Szliu static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */ 9424591Szliu double scalb(),copysign(),exp(),exp__E(),t; 9524591Szliu 9624591Szliu #ifndef VAX 9724591Szliu if(x!=x) return(x); /* x is NaN */ 9824591Szliu #endif 9924591Szliu if((x=copysign(x,one)) <= 22) 10024591Szliu if(x<0.3465) 10124591Szliu if(x<small) return(one+x); 10224591Szliu else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); } 10324591Szliu 10424591Szliu else /* for x lies in [0.3465,22] */ 10524591Szliu { t=exp(x); return((t+one/t)*half); } 10624591Szliu 10724591Szliu if( lnovfl <= x && x <= (lnovfl+0.7)) 10824591Szliu /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1)) 10924591Szliu * and return 2^max*exp(x) to avoid unnecessary overflow 11024591Szliu */ 11124591Szliu return(scalb(exp((x-mln2hi)-mln2lo), max)); 11224591Szliu 11324591Szliu else 11424591Szliu return(exp(x)*half); /* for large x, cosh(x)=exp(x)/2 */ 11524591Szliu } 116