Real floating and integer
When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined. Results of some implicit conversions may be represented in greater range and precision than that required by the new type (see Usual arithmetic conversions and Return statement).
[edit] Notes
The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1,Utype_MAX+1).
[edit] Example
#include <stdio.h> #include <math.h> #include <float.h> int main(void) { /* Value is truncated toward zero. */ double d = 1.5; int i = d; printf("%d\n", i); d = -1.5; i = d; printf("%d\n", i); /* The value of the integral part cannot be represented by the integer type. */ /* undefined behavior */ i = DBL_MAX; printf("%d\n", i); /* The integral value being converted can be represented exactly in the new type. */ /* The value is unchanged in the new type. */ d = 8; printf("%f\n", d); /* The integral value being converted is in the range of values that can be */ /* represented in the new type but cannot be represented exactly. */ /* implementation defined */ /* Double can represent a 53-bit integer exactly ... */ long int li = 0x1fffffffffffff; printf("%ld\n", li); d = li; printf("%f\n", d); /* but not a 54-bit integer. */ li = 0x3fffffffffffff; printf("%ld\n", li); d = li; printf("%f\n", d); return 0; }
Possible output:
1 -1 2147483647 8.000000 9007199254740991 9007199254740991.000000 18014398509481983 18014398509481984.000000