134124Sbostic /* 224591Szliu * Copyright (c) 1985 Regents of the University of California. 334124Sbostic * All rights reserved. 434124Sbostic * 5*42657Sbostic * %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. 1124591Szliu */ 1224591Szliu 1324591Szliu #ifndef lint 14*42657Sbostic static char sccsid[] = "@(#)cosh.c 5.5 (Berkeley) 06/01/90"; 1534124Sbostic #endif /* not lint */ 1624591Szliu 1724591Szliu /* COSH(X) 1824591Szliu * RETURN THE HYPERBOLIC COSINE OF X 1924591Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2024591Szliu * CODED IN C BY K.C. NG, 1/8/85; 2124591Szliu * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85. 2224591Szliu * 2324591Szliu * Required system supported functions : 2424591Szliu * copysign(x,y) 2524591Szliu * scalb(x,N) 2624591Szliu * 2724591Szliu * Required kernel function: 2824591Szliu * exp(x) 2924591Szliu * exp__E(x,c) ...return exp(x+c)-1-x for |x|<0.3465 3024591Szliu * 3124591Szliu * Method : 3224591Szliu * 1. Replace x by |x|. 3324591Szliu * 2. 3424591Szliu * [ exp(x) - 1 ]^2 3524591Szliu * 0 <= x <= 0.3465 : cosh(x) := 1 + ------------------- 3624591Szliu * 2*exp(x) 3724591Szliu * 3824591Szliu * exp(x) + 1/exp(x) 3924591Szliu * 0.3465 <= x <= 22 : cosh(x) := ------------------- 4024591Szliu * 2 4124591Szliu * 22 <= x <= lnovfl : cosh(x) := exp(x)/2 4224591Szliu * lnovfl <= x <= lnovfl+log(2) 4324591Szliu * : cosh(x) := exp(x)/2 (avoid overflow) 4424591Szliu * log(2)+lnovfl < x < INF: overflow to INF 4524591Szliu * 4624591Szliu * Note: .3465 is a number near one half of ln2. 4724591Szliu * 4824591Szliu * Special cases: 4924591Szliu * cosh(x) is x if x is +INF, -INF, or NaN. 5024591Szliu * only cosh(0)=1 is exact for finite x. 5124591Szliu * 5224591Szliu * Accuracy: 5324591Szliu * cosh(x) returns the exact hyperbolic cosine of x nearly rounded. 5424591Szliu * In a test run with 768,000 random arguments on a VAX, the maximum 5524591Szliu * observed error was 1.23 ulps (units in the last place). 5624591Szliu * 5724591Szliu * Constants: 5824591Szliu * The hexadecimal values are the intended ones for the following constants. 5924591Szliu * The decimal values may be used, provided that the compiler will convert 6024591Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6124591Szliu * shown. 6224591Szliu */ 6324591Szliu 6435679Sbostic #include "mathimpl.h" 6524591Szliu 6635679Sbostic vc(mln2hi, 8.8029691931113054792E1 ,0f33,43b0,2bdb,c7e2, 7, .B00F33C7E22BDB) 6735679Sbostic vc(mln2lo,-4.9650192275318476525E-16 ,1b60,a70f,582a,279e, -50,-.8F1B60279E582A) 6835679Sbostic vc(lnovfl, 8.8029691931113053016E1 ,0f33,43b0,2bda,c7e2, 7, .B00F33C7E22BDA) 6935679Sbostic 7035679Sbostic ic(mln2hi, 7.0978271289338397310E2, 10, 1.62E42FEFA39EF) 7135679Sbostic ic(mln2lo, 2.3747039373786107478E-14, -45, 1.ABC9E3B39803F) 7235679Sbostic ic(lnovfl, 7.0978271289338397310E2, 9, 1.62E42FEFA39EF) 7335679Sbostic 7435679Sbostic #ifdef vccast 7535679Sbostic #define mln2hi vccast(mln2hi) 7635679Sbostic #define mln2lo vccast(mln2lo) 7735679Sbostic #define lnovfl vccast(lnovfl) 7835679Sbostic #endif 7935679Sbostic 8031853Szliu #if defined(vax)||defined(tahoe) 8124591Szliu static max = 126 ; 8231853Szliu #else /* defined(vax)||defined(tahoe) */ 8324591Szliu static max = 1023 ; 8431853Szliu #endif /* defined(vax)||defined(tahoe) */ 8524591Szliu 8624591Szliu double cosh(x) 8724591Szliu double x; 8824591Szliu { 8935679Sbostic static const double half=1.0/2.0, 9035679Sbostic one=1.0, small=1.0E-18; /* fl(1+small)==1 */ 9135679Sbostic double t; 9224591Szliu 9331853Szliu #if !defined(vax)&&!defined(tahoe) 9424591Szliu if(x!=x) return(x); /* x is NaN */ 9531853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 9624591Szliu if((x=copysign(x,one)) <= 22) 9724591Szliu if(x<0.3465) 9824591Szliu if(x<small) return(one+x); 9924591Szliu else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); } 10024591Szliu 10124591Szliu else /* for x lies in [0.3465,22] */ 10224591Szliu { t=exp(x); return((t+one/t)*half); } 10324591Szliu 10424591Szliu if( lnovfl <= x && x <= (lnovfl+0.7)) 10524591Szliu /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1)) 10624591Szliu * and return 2^max*exp(x) to avoid unnecessary overflow 10724591Szliu */ 10824591Szliu return(scalb(exp((x-mln2hi)-mln2lo), max)); 10924591Szliu 11024591Szliu else 11124591Szliu return(exp(x)*half); /* for large x, cosh(x)=exp(x)/2 */ 11224591Szliu } 113