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