sinh.c 837 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "libm.h"
  2. /* sinh(x) = (exp(x) - 1/exp(x))/2
  3. * = (exp(x)-1 + (exp(x)-1)/exp(x))/2
  4. * = x + x^3/6 + o(x^5)
  5. */
  6. double sinh(double x)
  7. {
  8. union {double f; uint64_t i;} u = {.f = x};
  9. uint32_t w;
  10. double t, h, absx;
  11. h = 0.5;
  12. if (u.i >> 63)
  13. h = -h;
  14. /* |x| */
  15. u.i &= (uint64_t)-1/2;
  16. absx = u.f;
  17. w = u.i >> 32;
  18. /* |x| < log(DBL_MAX) */
  19. if (w < 0x40862e42) {
  20. t = expm1(absx);
  21. if (w < 0x3ff00000) {
  22. if (w < 0x3ff00000 - (26<<20))
  23. /* note: inexact and underflow are raised by expm1 */
  24. /* note: this branch avoids spurious underflow */
  25. return x;
  26. return h*(2*t - t*t/(t+1));
  27. }
  28. /* note: |x|>log(0x1p26)+eps could be just h*exp(x) */
  29. return h*(t + t/(t+1));
  30. }
  31. /* |x| > log(DBL_MAX) or nan */
  32. /* note: the result is stored to handle overflow */
  33. t = 2*h*__expo2(absx);
  34. return t;
  35. }