1*24591Szliu /* 2*24591Szliu * Copyright (c) 1985 Regents of the University of California. 3*24591Szliu * 4*24591Szliu * Use and reproduction of this software are granted in accordance with 5*24591Szliu * the terms and conditions specified in the Berkeley Software License 6*24591Szliu * Agreement (in particular, this entails acknowledgement of the programs' 7*24591Szliu * source, and inclusion of this notice) with the additional understanding 8*24591Szliu * that all recipients should regard themselves as participants in an 9*24591Szliu * ongoing research project and hence should feel obligated to report 10*24591Szliu * their experiences (good or bad) with these elementary function codes, 11*24591Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12*24591Szliu */ 13*24591Szliu 14*24591Szliu #ifndef lint 15*24591Szliu static char sccsid[] = "@(#)cosh.c 1.1 (ELEFUNT) 09/06/85"; 16*24591Szliu #endif not lint 17*24591Szliu 18*24591Szliu /* COSH(X) 19*24591Szliu * RETURN THE HYPERBOLIC COSINE OF X 20*24591Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 21*24591Szliu * CODED IN C BY K.C. NG, 1/8/85; 22*24591Szliu * REVISED BY K.C. NG on 2/8/85, 2/23/85, 3/7/85, 3/29/85, 4/16/85. 23*24591Szliu * 24*24591Szliu * Required system supported functions : 25*24591Szliu * copysign(x,y) 26*24591Szliu * scalb(x,N) 27*24591Szliu * 28*24591Szliu * Required kernel function: 29*24591Szliu * exp(x) 30*24591Szliu * exp__E(x,c) ...return exp(x+c)-1-x for |x|<0.3465 31*24591Szliu * 32*24591Szliu * Method : 33*24591Szliu * 1. Replace x by |x|. 34*24591Szliu * 2. 35*24591Szliu * [ exp(x) - 1 ]^2 36*24591Szliu * 0 <= x <= 0.3465 : cosh(x) := 1 + ------------------- 37*24591Szliu * 2*exp(x) 38*24591Szliu * 39*24591Szliu * exp(x) + 1/exp(x) 40*24591Szliu * 0.3465 <= x <= 22 : cosh(x) := ------------------- 41*24591Szliu * 2 42*24591Szliu * 22 <= x <= lnovfl : cosh(x) := exp(x)/2 43*24591Szliu * lnovfl <= x <= lnovfl+log(2) 44*24591Szliu * : cosh(x) := exp(x)/2 (avoid overflow) 45*24591Szliu * log(2)+lnovfl < x < INF: overflow to INF 46*24591Szliu * 47*24591Szliu * Note: .3465 is a number near one half of ln2. 48*24591Szliu * 49*24591Szliu * Special cases: 50*24591Szliu * cosh(x) is x if x is +INF, -INF, or NaN. 51*24591Szliu * only cosh(0)=1 is exact for finite x. 52*24591Szliu * 53*24591Szliu * Accuracy: 54*24591Szliu * cosh(x) returns the exact hyperbolic cosine of x nearly rounded. 55*24591Szliu * In a test run with 768,000 random arguments on a VAX, the maximum 56*24591Szliu * observed error was 1.23 ulps (units in the last place). 57*24591Szliu * 58*24591Szliu * Constants: 59*24591Szliu * The hexadecimal values are the intended ones for the following constants. 60*24591Szliu * The decimal values may be used, provided that the compiler will convert 61*24591Szliu * from decimal to binary accurately enough to produce the hexadecimal values 62*24591Szliu * shown. 63*24591Szliu */ 64*24591Szliu 65*24591Szliu #ifdef VAX 66*24591Szliu /* double static */ 67*24591Szliu /* mln2hi = 8.8029691931113054792E1 , Hex 2^ 7 * .B00F33C7E22BDB */ 68*24591Szliu /* mln2lo = -4.9650192275318476525E-16 , Hex 2^-50 * -.8F1B60279E582A */ 69*24591Szliu /* lnovfl = 8.8029691931113053016E1 ; Hex 2^ 7 * .B00F33C7E22BDA */ 70*24591Szliu static long mln2hix[] = { 0x0f3343b0, 0x2bdbc7e2}; 71*24591Szliu static long mln2lox[] = { 0x1b60a70f, 0x582a279e}; 72*24591Szliu static long lnovflx[] = { 0x0f3343b0, 0x2bdac7e2}; 73*24591Szliu #define mln2hi (*(double*)mln2hix) 74*24591Szliu #define mln2lo (*(double*)mln2lox) 75*24591Szliu #define lnovfl (*(double*)lnovflx) 76*24591Szliu #else /* IEEE double */ 77*24591Szliu double static 78*24591Szliu mln2hi = 7.0978271289338397310E2 , /*Hex 2^ 10 * 1.62E42FEFA39EF */ 79*24591Szliu mln2lo = 2.3747039373786107478E-14 , /*Hex 2^-45 * 1.ABC9E3B39803F */ 80*24591Szliu lnovfl = 7.0978271289338397310E2 ; /*Hex 2^ 9 * 1.62E42FEFA39EF */ 81*24591Szliu #endif 82*24591Szliu 83*24591Szliu #ifdef VAX 84*24591Szliu static max = 126 ; 85*24591Szliu #else /* IEEE double */ 86*24591Szliu static max = 1023 ; 87*24591Szliu #endif 88*24591Szliu 89*24591Szliu double cosh(x) 90*24591Szliu double x; 91*24591Szliu { 92*24591Szliu static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */ 93*24591Szliu double scalb(),copysign(),exp(),exp__E(),t; 94*24591Szliu 95*24591Szliu #ifndef VAX 96*24591Szliu if(x!=x) return(x); /* x is NaN */ 97*24591Szliu #endif 98*24591Szliu if((x=copysign(x,one)) <= 22) 99*24591Szliu if(x<0.3465) 100*24591Szliu if(x<small) return(one+x); 101*24591Szliu else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); } 102*24591Szliu 103*24591Szliu else /* for x lies in [0.3465,22] */ 104*24591Szliu { t=exp(x); return((t+one/t)*half); } 105*24591Szliu 106*24591Szliu if( lnovfl <= x && x <= (lnovfl+0.7)) 107*24591Szliu /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1)) 108*24591Szliu * and return 2^max*exp(x) to avoid unnecessary overflow 109*24591Szliu */ 110*24591Szliu return(scalb(exp((x-mln2hi)-mln2lo), max)); 111*24591Szliu 112*24591Szliu else 113*24591Szliu return(exp(x)*half); /* for large x, cosh(x)=exp(x)/2 */ 114*24591Szliu } 115