e_coshl.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "math_private_openbsd.h"
  35. static const long double one = 1.0, half=0.5, huge = 1.0e4900L;
  36. long double
  37. coshl(long double x)
  38. {
  39. long double t,w;
  40. int32_t ex;
  41. u_int32_t mx,lx;
  42. /* High word of |x|. */
  43. GET_LDOUBLE_WORDS(ex,mx,lx,x);
  44. ex &= 0x7fff;
  45. /* x is INF or NaN */
  46. if(ex==0x7fff) return x*x;
  47. /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expl(|x|)) */
  48. if(ex < 0x3ffd || (ex == 0x3ffd && mx < 0xb17217f7u)) {
  49. t = expm1l(fabsl(x));
  50. w = one+t;
  51. if (ex<0x3fbc) 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 (ex < 0x4003 || (ex == 0x4003 && mx < 0xb0000000u)) {
  56. t = expl(fabsl(x));
  57. return half*t+half/t;
  58. }
  59. /* |x| in [22, ln(maxdouble)] return half*exp(|x|) */
  60. if (ex < 0x400c || (ex == 0x400c && mx < 0xb1700000u))
  61. return half*expl(fabsl(x));
  62. /* |x| in [log(maxdouble), log(2*maxdouble)) */
  63. if (ex == 0x400c && (mx < 0xb174ddc0u
  64. || (mx == 0xb174ddc0u && lx < 0x31aec0ebu)))
  65. {
  66. w = expl(half*fabsl(x));
  67. t = half*w;
  68. return t*w;
  69. }
  70. /* |x| >= log(2*maxdouble), cosh(x) overflow */
  71. return huge*huge;
  72. }