cosh.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_cosh.c */
  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. /* 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 "libm.h"
  33. static const double huge = 1.0e300;
  34. double cosh(double x)
  35. {
  36. double t, w;
  37. int32_t ix;
  38. GET_HIGH_WORD(ix, x);
  39. ix &= 0x7fffffff;
  40. /* x is INF or NaN */
  41. if (ix >= 0x7ff00000)
  42. return x*x;
  43. /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
  44. if (ix < 0x3fd62e43) {
  45. t = expm1(fabs(x));
  46. w = 1.0+t;
  47. if (ix < 0x3c800000)
  48. return w; /* cosh(tiny) = 1 */
  49. return 1.0 + (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 = exp(fabs(x));
  54. return 0.5*t + 0.5/t;
  55. }
  56. /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */
  57. if (ix < 0x40862E42)
  58. return 0.5*exp(fabs(x));
  59. /* |x| in [log(maxdouble), overflowthresold] */
  60. if (ix <= 0x408633CE)
  61. return __expo2(fabs(x));
  62. /* |x| > overflowthresold, cosh(x) overflow */
  63. return huge*huge;
  64. }