[ create a new paste ] login | about

Link: http://codepad.org/tlzcufwh    [ raw code | output | fork ]

C, pasted on Oct 3:
    #include <stddef.h>
    #include <stdint.h>
    #include <stdio.h>
    #define min(a, b) (a < b ? a : b)
    #define bits(type) (sizeof(type) * 8)
    #define testimax(test_t) { \
      uintmax_t in = 1, out = 2; \
      size_t pow = 0, limit = min(bits(test_t), bits(uintmax_t)); \
      test_t t; \
      while (pow < limit && out == in + 1) { \
        in = in << 1; \
        out = (test_t) in + 1; \
        ++pow; \
      } \
      if (pow == limit) \
        puts(#test_t " is as precise as longest integer type"); \
      else printf(#test_t " conversion imprecise for 2^%d+1:\n" \
        "   in: %llu\n  out: %llu\n\n", pow, in + 1, out); \
    }

    int main(void)
    {
        testimax(float);
        testimax(double);
        return 0;
    }


Output:
1
2
3
4
5
6
7
8
float conversion imprecise for 2^24+1:
   in: 16777217
  out: 16777216

double conversion imprecise for 2^53+1:
   in: 9007199254740993
  out: 9007199254740992



Create a new paste based on this one


Comments: