e_cosh.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. libm_hidden_proto(expm1)
  38. libm_hidden_proto(fabs)
  39. #ifdef __STDC__
  40. static const double one = 1.0, half=0.5, huge = 1.0e300;
  41. #else
  42. static double one = 1.0, half=0.5, huge = 1.0e300;
  43. #endif
  44. #ifdef __STDC__
  45. double attribute_hidden __ieee754_cosh(double x)
  46. #else
  47. double attribute_hidden __ieee754_cosh(x)
  48. double x;
  49. #endif
  50. {
  51. double t,w;
  52. int32_t ix;
  53. u_int32_t lx;
  54. /* High word of |x|. */
  55. GET_HIGH_WORD(ix,x);
  56. ix &= 0x7fffffff;
  57. /* x is INF or NaN */
  58. if(ix>=0x7ff00000) return x*x;
  59. /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
  60. if(ix<0x3fd62e43) {
  61. t = expm1(fabs(x));
  62. w = one+t;
  63. if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */
  64. return one+(t*t)/(w+w);
  65. }
  66. /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
  67. if (ix < 0x40360000) {
  68. t = __ieee754_exp(fabs(x));
  69. return half*t+half/t;
  70. }
  71. /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
  72. if (ix < 0x40862E42) return half*__ieee754_exp(fabs(x));
  73. /* |x| in [log(maxdouble), overflowthresold] */
  74. GET_LOW_WORD(lx,x);
  75. if (ix<0x408633CE ||
  76. ((ix==0x408633ce)&&(lx<=(u_int32_t)0x8fb9f87d))) {
  77. w = __ieee754_exp(half*fabs(x));
  78. t = half*w;
  79. return t*w;
  80. }
  81. /* |x| > overflowthresold, cosh(x) overflow */
  82. return huge*huge;
  83. }