e_cosh.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* @(#)e_cosh.c 5.1 93/09/24 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, 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. #if defined(LIBM_SCCS) && !defined(lint)
  13. static char rcsid[] = "$NetBSD: e_cosh.c,v 1.7 1995/05/10 20:44:58 jtc Exp $";
  14. #endif
  15. /* __ieee754_cosh(x)
  16. * Method :
  17. * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
  18. * 1. Replace x by |x| (cosh(x) = cosh(-x)).
  19. * 2.
  20. * [ exp(x) - 1 ]^2
  21. * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
  22. * 2*exp(x)
  23. *
  24. * exp(x) + 1/exp(x)
  25. * ln2/2 <= x <= 22 : cosh(x) := -------------------
  26. * 2
  27. * 22 <= x <= lnovft : cosh(x) := exp(x)/2
  28. * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
  29. * ln2ovft < x : cosh(x) := huge*huge (overflow)
  30. *
  31. * Special cases:
  32. * cosh(x) is |x| if x is +INF, -INF, or NaN.
  33. * only cosh(0)=1 is exact for finite x.
  34. */
  35. #include "math.h"
  36. #include "math_private.h"
  37. #ifdef __STDC__
  38. static const double one = 1.0, half=0.5, huge = 1.0e300;
  39. #else
  40. static double one = 1.0, half=0.5, huge = 1.0e300;
  41. #endif
  42. #ifdef __STDC__
  43. double attribute_hidden __ieee754_cosh(double x)
  44. #else
  45. double attribute_hidden __ieee754_cosh(x)
  46. double x;
  47. #endif
  48. {
  49. double t,w;
  50. int32_t ix;
  51. u_int32_t lx;
  52. /* High word of |x|. */
  53. GET_HIGH_WORD(ix,x);
  54. ix &= 0x7fffffff;
  55. /* x is INF or NaN */
  56. if(ix>=0x7ff00000) return x*x;
  57. /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
  58. if(ix<0x3fd62e43) {
  59. t = expm1(fabs(x));
  60. w = one+t;
  61. if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */
  62. return one+(t*t)/(w+w);
  63. }
  64. /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
  65. if (ix < 0x40360000) {
  66. t = __ieee754_exp(fabs(x));
  67. return half*t+half/t;
  68. }
  69. /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
  70. if (ix < 0x40862E42) return half*__ieee754_exp(fabs(x));
  71. /* |x| in [log(maxdouble), overflowthresold] */
  72. GET_LOW_WORD(lx,x);
  73. if (ix<0x408633CE ||
  74. ((ix==0x408633ce)&&(lx<=(u_int32_t)0x8fb9f87d))) {
  75. w = __ieee754_exp(half*fabs(x));
  76. t = half*w;
  77. return t*w;
  78. }
  79. /* |x| > overflowthresold, cosh(x) overflow */
  80. return huge*huge;
  81. }