inf_torture.c 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main();
  4. int main2();
  5. int main3();
  6. int main4();
  7. int main() {
  8. printf("+inf:\n");
  9. float fx = (float)INFINITY; unsigned int *fxi = (unsigned int*)&fx;
  10. double dx = (double)INFINITY; long unsigned long int *dxi = (long unsigned long int*)&dx;
  11. long double ldx = (long double)INFINITY; long unsigned long int *ldxi1 = (long unsigned long int*)&ldx; long unsigned long int *ldxi2 = &(ldxi1[1]);
  12. printf("\t\tf d ld\n");
  13. printf("correct:\t%x %x %x\n", isinf(fx), isinf(dx), isinf(ldx));
  14. printf("as floats:\t%x %x %x\n", isinf(*(float*)fxi), isinf(*(float*)dxi), isinf(*(float*)ldxi1));
  15. printf("as double:\t%x %x %x\n", isinf(*(double*)fxi), isinf(*(double*)dxi), isinf(*(double*)ldxi1));
  16. printf("as long double:\t%x %x %x\n", isinf(*(long double*)fxi), isinf(*(long double*)dxi), isinf(*(long double*)ldxi1));
  17. printf("sizes ?4 8 12?:\t%d %d %d\n", (int)sizeof(fx), (int)sizeof(dx), (int)sizeof(ldx));
  18. printf("sizes:\t%d %d %d\n", (int)sizeof(*fxi), (int)sizeof(*dxi), (int)sizeof(*ldxi1)*2);
  19. printf("bit repr:\n f: %x\n d: %llx\n ld: %llx%llx\n", *fxi, *dxi, (0xFFFF)&*ldxi2, *ldxi1);
  20. printf("\n");
  21. main2();
  22. return 0;
  23. }
  24. int main2() {
  25. printf("-inf:\n");
  26. float fx = (float)-INFINITY; unsigned int *fxi = (unsigned int*)&fx;
  27. double dx = (double)-INFINITY; long unsigned long int *dxi = (long unsigned long int*)&dx;
  28. long double ldx = (long double)-INFINITY; long unsigned long int *ldxi1 = (long unsigned long int*)&ldx; long unsigned long int *ldxi2 = &(ldxi1[1]);
  29. printf("\t\tf d ld\n");
  30. printf("correct:\t%x %x %x\n", isinf(fx), isinf(dx), isinf(ldx));
  31. printf("as floats:\t%x %x %x\n", isinf(*(float*)fxi), isinf(*(float*)dxi), isinf(*(float*)ldxi1));
  32. printf("as double:\t%x %x %x\n", isinf(*(double*)fxi), isinf(*(double*)dxi), isinf(*(double*)ldxi1));
  33. printf("as long double:\t%x %x %x\n", isinf(*(long double*)fxi), isinf(*(long double*)dxi), isinf(*(long double*)ldxi1));
  34. printf("sizes ?4 8 12?:\t%d %d %d\n", (int)sizeof(fx), (int)sizeof(dx), (int)sizeof(ldx));
  35. printf("bit repr:\n f: %x\n d: %llx\n ld: %llx%llx\n", *fxi, *dxi, (0xFFFF)&*ldxi2, *ldxi1);
  36. printf("\n");
  37. main3();
  38. return 0;
  39. }
  40. int main3() {
  41. printf("+NaN:\n");
  42. float fx = (float)NAN; unsigned int *fxi = (unsigned int*)&fx;
  43. double dx = (double)NAN; long unsigned long int *dxi = (long unsigned long int*)&dx;
  44. long double ldx = (long double)NAN; long unsigned long int *ldxi1 = (long unsigned long int*)&ldx; long unsigned long int *ldxi2 = &(ldxi1[1]);
  45. printf("\t\tf d ld\n");
  46. printf("correct:\t%x %x %x\n", isnan(fx), isnan(dx), isnan(ldx));
  47. printf("as floats:\t%x %x %x\n", isnan(*(float*)fxi), isnan(*(float*)dxi), isnan(*(float*)ldxi1));
  48. printf("as double:\t%x %x %x\n", isnan(*(double*)fxi), isnan(*(double*)dxi), isnan(*(double*)ldxi1));
  49. printf("as long double:\t%x %x %x\n", isnan(*(long double*)fxi), isnan(*(long double*)dxi), isnan(*(long double*)ldxi1));
  50. printf("sizes ?4 8 12?:\t%d %d %d\n", (int)sizeof(fx), (int)sizeof(dx), (int)sizeof(ldx));
  51. printf("sizes:\t%d %d %d\n", (int)sizeof(*fxi), (int)sizeof(*dxi), (int)sizeof(*ldxi1)*2);
  52. printf("bit repr:\n f: %x\n d: %llx\n ld: %llx%llx\n", *fxi, *dxi, (0xFFFF)&*ldxi2, *ldxi1);
  53. printf("\n");
  54. main4();
  55. return 0;
  56. }
  57. int main4() {
  58. printf("-NaN:\n");
  59. float fx = (float)-NAN; unsigned int *fxi = (unsigned int*)&fx;
  60. double dx = (double)-NAN; long unsigned long int *dxi = (long unsigned long int*)&dx;
  61. long double ldx = (long double)-NAN; long unsigned long int *ldxi1 = (long unsigned long int*)&ldx; long unsigned long int *ldxi2 = &(ldxi1[1]);
  62. printf("\t\tf d ld\n");
  63. printf("correct:\t%x %x %x\n", isnan(fx), isnan(dx), isnan(ldx));
  64. printf("as floats:\t%x %x %x\n", isnan(*(float*)fxi), isnan(*(float*)dxi), isnan(*(float*)ldxi1));
  65. printf("as double:\t%x %x %x\n", isnan(*(double*)fxi), isnan(*(double*)dxi), isnan(*(double*)ldxi1));
  66. printf("as long double:\t%x %x %x\n", isnan(*(long double*)fxi), isnan(*(long double*)dxi), isnan(*(long double*)ldxi1));
  67. printf("sizes ?4 8 12?:\t%d %d %d\n", (int)sizeof(fx), (int)sizeof(dx), (int)sizeof(ldx));
  68. printf("bit repr:\n f: %x\n d: %llx\n ld: %llx%llx\n", *fxi, *dxi, (0xFFFF)&*ldxi2, *ldxi1);
  69. printf("\n");
  70. return 0;
  71. }