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