e_cosh.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/e_cosh.c,v 1.10 2011/10/21 06:28:47 das Exp $");
  14. /* __ieee754_cosh(x)
  15. * Method :
  16. * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
  17. * 1. Replace x by |x| (cosh(x) = cosh(-x)).
  18. * 2.
  19. * [ exp(x) - 1 ]^2
  20. * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
  21. * 2*exp(x)
  22. *
  23. * exp(x) + 1/exp(x)
  24. * ln2/2 <= x <= 22 : cosh(x) := -------------------
  25. * 2
  26. * 22 <= x <= lnovft : cosh(x) := exp(x)/2
  27. * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
  28. * ln2ovft < x : cosh(x) := huge*huge (overflow)
  29. *
  30. * Special cases:
  31. * cosh(x) is |x| if x is +INF, -INF, or NaN.
  32. * only cosh(0)=1 is exact for finite x.
  33. */
  34. #include <openlibm_math.h>
  35. #include "math_private.h"
  36. static const double one = 1.0, half=0.5, huge = 1.0e300;
  37. DLLEXPORT double
  38. __ieee754_cosh(double x)
  39. {
  40. double t,w;
  41. int32_t ix;
  42. /* High word of |x|. */
  43. GET_HIGH_WORD(ix,x);
  44. ix &= 0x7fffffff;
  45. /* x is INF or NaN */
  46. if(ix>=0x7ff00000) return x*x;
  47. /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
  48. if(ix<0x3fd62e43) {
  49. t = expm1(fabs(x));
  50. w = one+t;
  51. if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */
  52. return one+(t*t)/(w+w);
  53. }
  54. /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
  55. if (ix < 0x40360000) {
  56. t = __ieee754_exp(fabs(x));
  57. return half*t+half/t;
  58. }
  59. /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
  60. if (ix < 0x40862E42) return half*__ieee754_exp(fabs(x));
  61. /* |x| in [log(maxdouble), overflowthresold] */
  62. if (ix<=0x408633CE)
  63. return __ldexp_exp(fabs(x), -1);
  64. /* |x| > overflowthresold, cosh(x) overflow */
  65. return huge*huge;
  66. }