...
This solution replaces the float
with a double
. Furthermore, it uses a static an assertion to guarantee that the double
type can represent any int
without loss of precision. (See DCL03INT19-C. Use a static assertion to test the value of a constant expression.)Correctly compute integer widths for the definition and rationale of the UWIDTH()
macro):
Code Block | ||||
---|---|---|---|---|
| ||||
#include <assert.h> #include <stdio.h> #include <float.h> #include <limits.h> static_assert(sizeof(int) * CHAR_BITint main() { assert(UWIDTH(INT_MAX) <= DBL_MANT_DIG); int main() { int big = 1234567890; double approx = big; printf("%d\n", (big - (int)approx)); return 0; } |
...