modf.c 593 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <math.h>
  2. #include <stdint.h>
  3. double modf(double x, double *iptr)
  4. {
  5. union {double x; uint64_t n;} u = {x};
  6. uint64_t mask;
  7. int e;
  8. e = (int)(u.n>>52 & 0x7ff) - 0x3ff;
  9. /* no fractional part */
  10. if (e >= 52) {
  11. *iptr = x;
  12. if (e == 0x400 && u.n<<12 != 0) /* nan */
  13. return x;
  14. u.n &= (uint64_t)1<<63;
  15. return u.x;
  16. }
  17. /* no integral part*/
  18. if (e < 0) {
  19. u.n &= (uint64_t)1<<63;
  20. *iptr = u.x;
  21. return x;
  22. }
  23. mask = (uint64_t)-1>>12 >> e;
  24. if ((u.n & mask) == 0) {
  25. *iptr = x;
  26. u.n &= (uint64_t)1<<63;
  27. return u.x;
  28. }
  29. u.n &= ~mask;
  30. *iptr = u.x;
  31. return x - *iptr;
  32. }