1 
2 /* @(#)e_cosh.c 5.1 93/09/24 */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13 
14 /* cosh(x)
15  * Method :
16  * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
17  *	1. Replace x by |x| (cosh(x) = cosh(-x)).
18  *	2.
19  *		                                        [ exp(x) - 1 ]^2
20  *	    0        <= x <= ln2/2  :  cosh(x) := 1 + -------------------
21  *			       			           2*exp(x)
22  *
23  *		                                  exp(x) +  1/exp(x)
24  *	    ln2/2    <= x <= 22     :  cosh(x) := -------------------
25  *			       			          2
26  *	    22       <= x <= lnovft :  cosh(x) := exp(x)/2
27  *	    lnovft   <= x <= ln2ovft:  cosh(x) := exp(x/2)/2 * exp(x/2)
28  *	    ln2ovft  <  x	    :  cosh(x) := overflow
29  *
30  * Special cases:
31  *	cosh(x) is |x| if x is +INF, -INF, or NaN.
32  *	only cosh(0)=1 is exact for finite x.
33  */
34 
35 #include "fdlibm.h"
36 
37 #ifdef _NEED_FLOAT64
38 
39 static const __float64 one = _F_64(1.0), half = _F_64(0.5);
40 
41 __float64
cosh64(__float64 x)42 cosh64(__float64 x)
43 {
44     __float64 t, w;
45     __int32_t ix;
46     __uint32_t lx;
47 
48     x = fabs64(x);
49 
50     /* High word of |x|. */
51     GET_HIGH_WORD(ix, x);
52     ix &= 0x7fffffff;
53 
54     /* x is INF or NaN */
55     if (ix >= 0x7ff00000)
56         return x + x;
57 
58     /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
59     if (ix < 0x3fd62e43) {
60         t = expm1(x);
61         w = one + t;
62         if (ix < 0x3c800000)
63             return w; /* cosh(tiny) = 1 */
64         return one + (t * t) / (w + w);
65     }
66 
67     /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
68     if (ix < 0x40360000) {
69         t = exp(x);
70         return half * t + half / t;
71     }
72 
73     /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
74     if (ix < 0x40862E42)
75         return half * exp(x);
76 
77     /* |x| in [log(maxdouble), overflowthresold] */
78     GET_LOW_WORD(lx, x);
79     if (ix < 0x408633CE || (ix == 0x408633ce && lx <= (__uint32_t)0x8fb9f87d)) {
80         w = exp(half * x);
81         t = half * w;
82         return t * w;
83     }
84 
85     /* |x| > overflowthresold, cosh(x) overflow */
86     return __math_oflow(0);
87 }
88 
89 _MATH_ALIAS_d_d(cosh)
90 
91 #endif /* _NEED_FLOAT64 */
92