134124Sbostic /* 224591Szliu * Copyright (c) 1985 Regents of the University of California. 334124Sbostic * All rights reserved. 434124Sbostic * 534124Sbostic * Redistribution and use in source and binary forms are permitted 634931Sbostic * provided that the above copyright notice and this paragraph are 734931Sbostic * duplicated in all such forms and that any documentation, 834931Sbostic * advertising materials, and other materials related to such 934931Sbostic * distribution and use acknowledge that the software was developed 1034931Sbostic * by the University of California, Berkeley. The name of the 1134931Sbostic * University may not be used to endorse or promote products derived 1234931Sbostic * from this software without specific prior written permission. 1334931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534931Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634124Sbostic * 1734124Sbostic * All recipients should regard themselves as participants in an ongoing 1834124Sbostic * research project and hence should feel obligated to report their 1934124Sbostic * experiences (good or bad) with these elementary function codes, using 2034124Sbostic * the sendbug(8) program, to the authors. 2124591Szliu */ 2224591Szliu 2324591Szliu #ifndef lint 24*35679Sbostic static char sccsid[] = "@(#)cosh.c 5.4 (Berkeley) 09/22/88"; 2534124Sbostic #endif /* not lint */ 2624591Szliu 2724591Szliu /* COSH(X) 2824591Szliu * RETURN THE HYPERBOLIC COSINE OF X 2924591Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 3024591Szliu * CODED IN C BY K.C. NG, 1/8/85; 3124591Szliu * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85. 3224591Szliu * 3324591Szliu * Required system supported functions : 3424591Szliu * copysign(x,y) 3524591Szliu * scalb(x,N) 3624591Szliu * 3724591Szliu * Required kernel function: 3824591Szliu * exp(x) 3924591Szliu * exp__E(x,c) ...return exp(x+c)-1-x for |x|<0.3465 4024591Szliu * 4124591Szliu * Method : 4224591Szliu * 1. Replace x by |x|. 4324591Szliu * 2. 4424591Szliu * [ exp(x) - 1 ]^2 4524591Szliu * 0 <= x <= 0.3465 : cosh(x) := 1 + ------------------- 4624591Szliu * 2*exp(x) 4724591Szliu * 4824591Szliu * exp(x) + 1/exp(x) 4924591Szliu * 0.3465 <= x <= 22 : cosh(x) := ------------------- 5024591Szliu * 2 5124591Szliu * 22 <= x <= lnovfl : cosh(x) := exp(x)/2 5224591Szliu * lnovfl <= x <= lnovfl+log(2) 5324591Szliu * : cosh(x) := exp(x)/2 (avoid overflow) 5424591Szliu * log(2)+lnovfl < x < INF: overflow to INF 5524591Szliu * 5624591Szliu * Note: .3465 is a number near one half of ln2. 5724591Szliu * 5824591Szliu * Special cases: 5924591Szliu * cosh(x) is x if x is +INF, -INF, or NaN. 6024591Szliu * only cosh(0)=1 is exact for finite x. 6124591Szliu * 6224591Szliu * Accuracy: 6324591Szliu * cosh(x) returns the exact hyperbolic cosine of x nearly rounded. 6424591Szliu * In a test run with 768,000 random arguments on a VAX, the maximum 6524591Szliu * observed error was 1.23 ulps (units in the last place). 6624591Szliu * 6724591Szliu * Constants: 6824591Szliu * The hexadecimal values are the intended ones for the following constants. 6924591Szliu * The decimal values may be used, provided that the compiler will convert 7024591Szliu * from decimal to binary accurately enough to produce the hexadecimal values 7124591Szliu * shown. 7224591Szliu */ 7324591Szliu 74*35679Sbostic #include "mathimpl.h" 7524591Szliu 76*35679Sbostic vc(mln2hi, 8.8029691931113054792E1 ,0f33,43b0,2bdb,c7e2, 7, .B00F33C7E22BDB) 77*35679Sbostic vc(mln2lo,-4.9650192275318476525E-16 ,1b60,a70f,582a,279e, -50,-.8F1B60279E582A) 78*35679Sbostic vc(lnovfl, 8.8029691931113053016E1 ,0f33,43b0,2bda,c7e2, 7, .B00F33C7E22BDA) 79*35679Sbostic 80*35679Sbostic ic(mln2hi, 7.0978271289338397310E2, 10, 1.62E42FEFA39EF) 81*35679Sbostic ic(mln2lo, 2.3747039373786107478E-14, -45, 1.ABC9E3B39803F) 82*35679Sbostic ic(lnovfl, 7.0978271289338397310E2, 9, 1.62E42FEFA39EF) 83*35679Sbostic 84*35679Sbostic #ifdef vccast 85*35679Sbostic #define mln2hi vccast(mln2hi) 86*35679Sbostic #define mln2lo vccast(mln2lo) 87*35679Sbostic #define lnovfl vccast(lnovfl) 88*35679Sbostic #endif 89*35679Sbostic 9031853Szliu #if defined(vax)||defined(tahoe) 9124591Szliu static max = 126 ; 9231853Szliu #else /* defined(vax)||defined(tahoe) */ 9324591Szliu static max = 1023 ; 9431853Szliu #endif /* defined(vax)||defined(tahoe) */ 9524591Szliu 9624591Szliu double cosh(x) 9724591Szliu double x; 9824591Szliu { 99*35679Sbostic static const double half=1.0/2.0, 100*35679Sbostic one=1.0, small=1.0E-18; /* fl(1+small)==1 */ 101*35679Sbostic double t; 10224591Szliu 10331853Szliu #if !defined(vax)&&!defined(tahoe) 10424591Szliu if(x!=x) return(x); /* x is NaN */ 10531853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 10624591Szliu if((x=copysign(x,one)) <= 22) 10724591Szliu if(x<0.3465) 10824591Szliu if(x<small) return(one+x); 10924591Szliu else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); } 11024591Szliu 11124591Szliu else /* for x lies in [0.3465,22] */ 11224591Szliu { t=exp(x); return((t+one/t)*half); } 11324591Szliu 11424591Szliu if( lnovfl <= x && x <= (lnovfl+0.7)) 11524591Szliu /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1)) 11624591Szliu * and return 2^max*exp(x) to avoid unnecessary overflow 11724591Szliu */ 11824591Szliu return(scalb(exp((x-mln2hi)-mln2lo), max)); 11924591Szliu 12024591Szliu else 12124591Szliu return(exp(x)*half); /* for large x, cosh(x)=exp(x)/2 */ 12224591Szliu } 123