e_cosh.c 2.4 KB

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