e_coshl.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /* coshl(x)
  13. * Method :
  14. * mathematically coshl(x) if defined to be (exp(x)+exp(-x))/2
  15. * 1. Replace x by |x| (coshl(x) = coshl(-x)).
  16. * 2.
  17. * [ exp(x) - 1 ]^2
  18. * 0 <= x <= ln2/2 : coshl(x) := 1 + -------------------
  19. * 2*exp(x)
  20. *
  21. * exp(x) + 1/exp(x)
  22. * ln2/2 <= x <= 22 : coshl(x) := -------------------
  23. * 2
  24. * 22 <= x <= lnovft : coshl(x) := expl(x)/2
  25. * lnovft <= x <= ln2ovft: coshl(x) := expl(x/2)/2 * expl(x/2)
  26. * ln2ovft < x : coshl(x) := huge*huge (overflow)
  27. *
  28. * Special cases:
  29. * coshl(x) is |x| if x is +INF, -INF, or NaN.
  30. * only coshl(0)=1 is exact for finite x.
  31. */
  32. #include <openlibm_math.h>
  33. #include "math_private.h"
  34. static const long double one = 1.0, half=0.5, huge = 1.0e4900L;
  35. long double
  36. coshl(long double x)
  37. {
  38. long double t,w;
  39. int32_t ex;
  40. u_int32_t mx,lx;
  41. /* High word of |x|. */
  42. GET_LDOUBLE_WORDS(ex,mx,lx,x);
  43. ex &= 0x7fff;
  44. /* x is INF or NaN */
  45. if(ex==0x7fff) return x*x;
  46. /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expl(|x|)) */
  47. if(ex < 0x3ffd || (ex == 0x3ffd && mx < 0xb17217f7u)) {
  48. t = expm1l(fabsl(x));
  49. w = one+t;
  50. if (ex<0x3fbc) return w; /* cosh(tiny) = 1 */
  51. return one+(t*t)/(w+w);
  52. }
  53. /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
  54. if (ex < 0x4003 || (ex == 0x4003 && mx < 0xb0000000u)) {
  55. t = expl(fabsl(x));
  56. return half*t+half/t;
  57. }
  58. /* |x| in [22, ln(maxdouble)] return half*exp(|x|) */
  59. if (ex < 0x400c || (ex == 0x400c && mx < 0xb1700000u))
  60. return half*expl(fabsl(x));
  61. /* |x| in [log(maxdouble), log(2*maxdouble)) */
  62. if (ex == 0x400c && (mx < 0xb174ddc0u
  63. || (mx == 0xb174ddc0u && lx < 0x31aec0ebu)))
  64. {
  65. w = expl(half*fabsl(x));
  66. t = half*w;
  67. return t*w;
  68. }
  69. /* |x| >= log(2*maxdouble), cosh(x) overflow */
  70. return huge*huge;
  71. }