13f708241SDavid Schultz
23a8617a8SJordan K. Hubbard /*
33a8617a8SJordan K. Hubbard * ====================================================
43a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
53a8617a8SJordan K. Hubbard *
63f708241SDavid Schultz * Developed at SunSoft, a Sun Microsystems, Inc. business.
73a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this
83a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice
93a8617a8SJordan K. Hubbard * is preserved.
103a8617a8SJordan K. Hubbard * ====================================================
113a8617a8SJordan K. Hubbard */
123a8617a8SJordan K. Hubbard
13*99843eb8SSteve Kargl /* cosh(x)
143a8617a8SJordan K. Hubbard * Method :
153a8617a8SJordan K. Hubbard * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
163a8617a8SJordan K. Hubbard * 1. Replace x by |x| (cosh(x) = cosh(-x)).
173a8617a8SJordan K. Hubbard * 2.
183a8617a8SJordan K. Hubbard * [ exp(x) - 1 ]^2
193a8617a8SJordan K. Hubbard * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
203a8617a8SJordan K. Hubbard * 2*exp(x)
213a8617a8SJordan K. Hubbard *
223a8617a8SJordan K. Hubbard * exp(x) + 1/exp(x)
233a8617a8SJordan K. Hubbard * ln2/2 <= x <= 22 : cosh(x) := -------------------
243a8617a8SJordan K. Hubbard * 2
253a8617a8SJordan K. Hubbard * 22 <= x <= lnovft : cosh(x) := exp(x)/2
263a8617a8SJordan K. Hubbard * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
273a8617a8SJordan K. Hubbard * ln2ovft < x : cosh(x) := huge*huge (overflow)
283a8617a8SJordan K. Hubbard *
293a8617a8SJordan K. Hubbard * Special cases:
303a8617a8SJordan K. Hubbard * cosh(x) is |x| if x is +INF, -INF, or NaN.
313a8617a8SJordan K. Hubbard * only cosh(0)=1 is exact for finite x.
323a8617a8SJordan K. Hubbard */
333a8617a8SJordan K. Hubbard
34a48e1f22SSteve Kargl #include <float.h>
35a48e1f22SSteve Kargl
363a8617a8SJordan K. Hubbard #include "math.h"
373a8617a8SJordan K. Hubbard #include "math_private.h"
383a8617a8SJordan K. Hubbard
393a8617a8SJordan K. Hubbard static const double one = 1.0, half=0.5, huge = 1.0e300;
403a8617a8SJordan K. Hubbard
41a82bbc73SAlfred Perlstein double
cosh(double x)42*99843eb8SSteve Kargl cosh(double x)
433a8617a8SJordan K. Hubbard {
443a8617a8SJordan K. Hubbard double t,w;
453a8617a8SJordan K. Hubbard int32_t ix;
463a8617a8SJordan K. Hubbard
473a8617a8SJordan K. Hubbard /* High word of |x|. */
483a8617a8SJordan K. Hubbard GET_HIGH_WORD(ix,x);
493a8617a8SJordan K. Hubbard ix &= 0x7fffffff;
503a8617a8SJordan K. Hubbard
513a8617a8SJordan K. Hubbard /* x is INF or NaN */
523a8617a8SJordan K. Hubbard if(ix>=0x7ff00000) return x*x;
533a8617a8SJordan K. Hubbard
543a8617a8SJordan K. Hubbard /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
553a8617a8SJordan K. Hubbard if(ix<0x3fd62e43) {
563a8617a8SJordan K. Hubbard t = expm1(fabs(x));
573a8617a8SJordan K. Hubbard w = one+t;
583a8617a8SJordan K. Hubbard if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */
593a8617a8SJordan K. Hubbard return one+(t*t)/(w+w);
603a8617a8SJordan K. Hubbard }
613a8617a8SJordan K. Hubbard
623a8617a8SJordan K. Hubbard /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
633a8617a8SJordan K. Hubbard if (ix < 0x40360000) {
64*99843eb8SSteve Kargl t = exp(fabs(x));
653a8617a8SJordan K. Hubbard return half*t+half/t;
663a8617a8SJordan K. Hubbard }
673a8617a8SJordan K. Hubbard
683a8617a8SJordan K. Hubbard /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
69*99843eb8SSteve Kargl if (ix < 0x40862E42) return half*exp(fabs(x));
703a8617a8SJordan K. Hubbard
713a8617a8SJordan K. Hubbard /* |x| in [log(maxdouble), overflowthresold] */
72d4657ac7SDavid Schultz if (ix<=0x408633CE)
73d4657ac7SDavid Schultz return __ldexp_exp(fabs(x), -1);
743a8617a8SJordan K. Hubbard
753a8617a8SJordan K. Hubbard /* |x| > overflowthresold, cosh(x) overflow */
763a8617a8SJordan K. Hubbard return huge*huge;
773a8617a8SJordan K. Hubbard }
78a48e1f22SSteve Kargl
79a48e1f22SSteve Kargl #if (LDBL_MANT_DIG == 53)
80a48e1f22SSteve Kargl __weak_reference(cosh, coshl);
81a48e1f22SSteve Kargl #endif
82