sinh.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_sinh.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. /* sinh(x)
  13. * Method :
  14. * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
  15. * 1. Replace x by |x| (sinh(-x) = -sinh(x)).
  16. * 2.
  17. * E + E/(E+1)
  18. * 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
  19. * 2
  20. *
  21. * 22 <= x <= lnovft : sinh(x) := exp(x)/2
  22. * lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
  23. * ln2ovft < x : sinh(x) := x*shuge (overflow)
  24. *
  25. * Special cases:
  26. * sinh(x) is |x| if x is +INF, -INF, or NaN.
  27. * only sinh(0)=0 is exact for finite x.
  28. */
  29. #include "libm.h"
  30. static const double huge = 1.0e307;
  31. double sinh(double x)
  32. {
  33. double t, h;
  34. int32_t ix, jx;
  35. /* High word of |x|. */
  36. GET_HIGH_WORD(jx, x);
  37. ix = jx & 0x7fffffff;
  38. /* x is INF or NaN */
  39. if (ix >= 0x7ff00000)
  40. return x + x;
  41. h = 0.5;
  42. if (jx < 0) h = -h;
  43. /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */
  44. if (ix < 0x40360000) { /* |x|<22 */
  45. if (ix < 0x3e300000) /* |x|<2**-28 */
  46. /* raise inexact, return x */
  47. if (huge+x > 1.0)
  48. return x;
  49. t = expm1(fabs(x));
  50. if (ix < 0x3ff00000)
  51. return h*(2.0*t - t*t/(t+1.0));
  52. return h*(t + t/(t+1.0));
  53. }
  54. /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */
  55. if (ix < 0x40862E42)
  56. return h*exp(fabs(x));
  57. /* |x| in [log(maxdouble), overflowthresold] */
  58. if (ix <= 0x408633CE)
  59. return h * 2.0 * __expo2(fabs(x)); /* h is for sign only */
  60. /* |x| > overflowthresold, sinh(x) overflow */
  61. return x*huge;
  62. }