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