xref: /csrg-svn/lib/libm/common_source/cosh.c (revision 34931)
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.3 (Berkeley) 06/30/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 #if defined(vax)||defined(tahoe)
75 #ifdef vax
76 #define _0x(A,B)	0x/**/A/**/B
77 #else	/* vax */
78 #define _0x(A,B)	0x/**/B/**/A
79 #endif	/* vax */
80 /* static double  */
81 /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
82 /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
83 /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
84 static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
85 static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
86 static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
87 #define   mln2hi    (*(double*)mln2hix)
88 #define   mln2lo    (*(double*)mln2lox)
89 #define   lnovfl    (*(double*)lnovflx)
90 #else	/* defined(vax)||defined(tahoe) */
91 static double
92 mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
93 mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
94 lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
95 #endif	/* defined(vax)||defined(tahoe) */
96 
97 #if defined(vax)||defined(tahoe)
98 static max = 126                      ;
99 #else	/* defined(vax)||defined(tahoe) */
100 static max = 1023                     ;
101 #endif	/* defined(vax)||defined(tahoe) */
102 
103 double cosh(x)
104 double x;
105 {
106 	static double half=1.0/2.0,one=1.0, small=1.0E-18; /* fl(1+small)==1 */
107 	double scalb(),copysign(),exp(),exp__E(),t;
108 
109 #if !defined(vax)&&!defined(tahoe)
110 	if(x!=x) return(x);	/* x is NaN */
111 #endif	/* !defined(vax)&&!defined(tahoe) */
112 	if((x=copysign(x,one)) <= 22)
113 	    if(x<0.3465)
114 		if(x<small) return(one+x);
115 		else {t=x+exp__E(x,0.0);x=t+t; return(one+t*t/(2.0+x)); }
116 
117 	    else /* for x lies in [0.3465,22] */
118 	        { t=exp(x); return((t+one/t)*half); }
119 
120 	if( lnovfl <= x && x <= (lnovfl+0.7))
121         /* for x lies in [lnovfl, lnovfl+ln2], decrease x by ln(2^(max+1))
122          * and return 2^max*exp(x) to avoid unnecessary overflow
123          */
124 	    return(scalb(exp((x-mln2hi)-mln2lo), max));
125 
126 	else
127 	    return(exp(x)*half);	/* for large x,  cosh(x)=exp(x)/2 */
128 }
129