e_coshl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  13. *
  14. * Permission to use, copy, modify, and distribute this software for any
  15. * purpose with or without fee is hereby granted, provided that the above
  16. * copyright notice and this permission notice appear in all copies.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  19. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  21. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  22. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  23. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  24. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. */
  26. /* coshl(x)
  27. * Method :
  28. * mathematically coshl(x) if defined to be (exp(x)+exp(-x))/2
  29. * 1. Replace x by |x| (coshl(x) = coshl(-x)).
  30. * 2.
  31. * [ exp(x) - 1 ]^2
  32. * 0 <= x <= ln2/2 : coshl(x) := 1 + -------------------
  33. * 2*exp(x)
  34. *
  35. * exp(x) + 1/exp(x)
  36. * ln2/2 <= x <= 22 : coshl(x) := -------------------
  37. * 2
  38. * 22 <= x <= lnovft : coshl(x) := expl(x)/2
  39. * lnovft <= x <= ln2ovft: coshl(x) := expl(x/2)/2 * expl(x/2)
  40. * ln2ovft < x : coshl(x) := huge*huge (overflow)
  41. *
  42. * Special cases:
  43. * coshl(x) is |x| if x is +INF, -INF, or NaN.
  44. * only coshl(0)=1 is exact for finite x.
  45. */
  46. #include <openlibm_math.h>
  47. #include "math_private.h"
  48. static const long double one = 1.0, half = 0.5, huge = 1.0e4900L,
  49. ovf_thresh = 1.1357216553474703894801348310092223067821E4L;
  50. long double
  51. coshl(long double x)
  52. {
  53. long double t, w;
  54. int32_t ex;
  55. ieee_quad_shape_type u;
  56. u.value = x;
  57. ex = u.parts32.mswhi & 0x7fffffff;
  58. /* Absolute value of x. */
  59. u.parts32.mswhi = ex;
  60. /* x is INF or NaN */
  61. if (ex >= 0x7fff0000)
  62. return x * x;
  63. /* |x| in [0,0.5*ln2], return 1+expm1l(|x|)^2/(2*expl(|x|)) */
  64. if (ex < 0x3ffd62e4) /* 0.3465728759765625 */
  65. {
  66. t = expm1l (u.value);
  67. w = one + t;
  68. if (ex < 0x3fb80000) /* |x| < 2^-116 */
  69. return w; /* cosh(tiny) = 1 */
  70. return one + (t * t) / (w + w);
  71. }
  72. /* |x| in [0.5*ln2,40], return (exp(|x|)+1/exp(|x|)/2; */
  73. if (ex < 0x40044000)
  74. {
  75. t = expl (u.value);
  76. return half * t + half / t;
  77. }
  78. /* |x| in [22, ln(maxdouble)] return half*exp(|x|) */
  79. if (ex <= 0x400c62e3) /* 11356.375 */
  80. return half * expl (u.value);
  81. /* |x| in [log(maxdouble), overflowthresold] */
  82. if (u.value <= ovf_thresh)
  83. {
  84. w = expl (half * u.value);
  85. t = half * w;
  86. return t * w;
  87. }
  88. /* |x| > overflowthresold, cosh(x) overflow */
  89. return huge * huge;
  90. }