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