e_cosh.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /* __ieee754_cosh(x)
  12. * Method :
  13. * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
  14. * 1. Replace x by |x| (cosh(x) = cosh(-x)).
  15. * 2.
  16. * [ exp(x) - 1 ]^2
  17. * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
  18. * 2*exp(x)
  19. *
  20. * exp(x) + 1/exp(x)
  21. * ln2/2 <= x <= 22 : cosh(x) := -------------------
  22. * 2
  23. * 22 <= x <= lnovft : cosh(x) := exp(x)/2
  24. * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
  25. * ln2ovft < x : cosh(x) := huge*huge (overflow)
  26. *
  27. * Special cases:
  28. * cosh(x) is |x| if x is +INF, -INF, or NaN.
  29. * only cosh(0)=1 is exact for finite x.
  30. */
  31. #include "math.h"
  32. #include "math_private.h"
  33. static const double one = 1.0, half=0.5, huge = 1.0e300;
  34. double attribute_hidden __ieee754_cosh(double x)
  35. {
  36. double t,w;
  37. int32_t ix;
  38. u_int32_t lx;
  39. /* High word of |x|. */
  40. GET_HIGH_WORD(ix,x);
  41. ix &= 0x7fffffff;
  42. /* x is INF or NaN */
  43. if(ix>=0x7ff00000) return x*x;
  44. /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
  45. if(ix<0x3fd62e43) {
  46. t = expm1(fabs(x));
  47. w = one+t;
  48. if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */
  49. return one+(t*t)/(w+w);
  50. }
  51. /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
  52. if (ix < 0x40360000) {
  53. t = __ieee754_exp(fabs(x));
  54. return half*t+half/t;
  55. }
  56. /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
  57. if (ix < 0x40862E42) return half*__ieee754_exp(fabs(x));
  58. /* |x| in [log(maxdouble), overflowthresold] */
  59. GET_LOW_WORD(lx,x);
  60. if (ix<0x408633CE ||
  61. ((ix==0x408633ce)&&(lx<=(u_int32_t)0x8fb9f87d))) {
  62. w = __ieee754_exp(half*fabs(x));
  63. t = half*w;
  64. return t*w;
  65. }
  66. /* |x| > overflowthresold, cosh(x) overflow */
  67. return huge*huge;
  68. }