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 6*34931Sbostic * provided that the above copyright notice and this paragraph are 7*34931Sbostic * duplicated in all such forms and that any documentation, 8*34931Sbostic * advertising materials, and other materials related to such 9*34931Sbostic * distribution and use acknowledge that the software was developed 10*34931Sbostic * by the University of California, Berkeley. The name of the 11*34931Sbostic * University may not be used to endorse or promote products derived 12*34931Sbostic * from this software without specific prior written permission. 13*34931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34931Sbostic * 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*34931Sbostic static char sccsid[] = "@(#)cosh.c 5.3 (Berkeley) 06/30/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 7431853Szliu #if defined(vax)||defined(tahoe) 7531853Szliu #ifdef vax 7631812Szliu #define _0x(A,B) 0x/**/A/**/B 7731853Szliu #else /* vax */ 7831812Szliu #define _0x(A,B) 0x/**/B/**/A 7931853Szliu #endif /* vax */ 8026893Selefunt /* static double */ 8124591Szliu /* mln2hi = 8.8029691931113054792E1 , Hex 2^ 7 * .B00F33C7E22BDB */ 8224591Szliu /* mln2lo = -4.9650192275318476525E-16 , Hex 2^-50 * -.8F1B60279E582A */ 8324591Szliu /* lnovfl = 8.8029691931113053016E1 ; Hex 2^ 7 * .B00F33C7E22BDA */ 8431812Szliu static long mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)}; 8531812Szliu static long mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)}; 8631812Szliu static long lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)}; 8724591Szliu #define mln2hi (*(double*)mln2hix) 8824591Szliu #define mln2lo (*(double*)mln2lox) 8924591Szliu #define lnovfl (*(double*)lnovflx) 9031853Szliu #else /* defined(vax)||defined(tahoe) */ 9126893Selefunt static double 9224591Szliu mln2hi = 7.0978271289338397310E2 , /*Hex 2^ 10 * 1.62E42FEFA39EF */ 9324591Szliu mln2lo = 2.3747039373786107478E-14 , /*Hex 2^-45 * 1.ABC9E3B39803F */ 9424591Szliu lnovfl = 7.0978271289338397310E2 ; /*Hex 2^ 9 * 1.62E42FEFA39EF */ 9531853Szliu #endif /* defined(vax)||defined(tahoe) */ 9624591Szliu 9731853Szliu #if defined(vax)||defined(tahoe) 9824591Szliu static max = 126 ; 9931853Szliu #else /* defined(vax)||defined(tahoe) */ 10024591Szliu static max = 1023 ; 10131853Szliu #endif /* defined(vax)||defined(tahoe) */ 10224591Szliu 10324591Szliu double cosh(x) 10424591Szliu double x; 10524591Szliu { 10624591Szliu static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */ 10724591Szliu double scalb(),copysign(),exp(),exp__E(),t; 10824591Szliu 10931853Szliu #if !defined(vax)&&!defined(tahoe) 11024591Szliu if(x!=x) return(x); /* x is NaN */ 11131853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 11224591Szliu if((x=copysign(x,one)) <= 22) 11324591Szliu if(x<0.3465) 11424591Szliu if(x<small) return(one+x); 11524591Szliu else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); } 11624591Szliu 11724591Szliu else /* for x lies in [0.3465,22] */ 11824591Szliu { t=exp(x); return((t+one/t)*half); } 11924591Szliu 12024591Szliu if( lnovfl <= x && x <= (lnovfl+0.7)) 12124591Szliu /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1)) 12224591Szliu * and return 2^max*exp(x) to avoid unnecessary overflow 12324591Szliu */ 12424591Szliu return(scalb(exp((x-mln2hi)-mln2lo), max)); 12524591Szliu 12624591Szliu else 12724591Szliu return(exp(x)*half); /* for large x, cosh(x)=exp(x)/2 */ 12824591Szliu } 129