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