codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#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; }
Private
[
?
]
Run code
Submit