01-10-2013, 12:15 PM
In my opinion, Rule 12.12 intends to prevent direct access to the representation of floating-point values. This might typically be achieved using either a union or pointer casts, for example:
I don't think that casting a floating-point value to int violates Rule 12.12 because there is no direct access to the representation of the value. In addition, the conversion is well-defined in that the floating point value is truncated towards zero with the result being undefined only if the truncated value won't fit in the integer type.
Code:
unsigned int tmp = *(unsigned int *)&f; /* get the float as 32-bit int */
tmp = tmp ^ 0x80000000; /* flip the sign bit */
f = *(float *)&tmp; /* store the result back */
I don't think that casting a floating-point value to int violates Rule 12.12 because there is no direct access to the representation of the value. In addition, the conversion is well-defined in that the floating point value is truncated towards zero with the result being undefined only if the truncated value won't fit in the integer type.
<t></t>