MISRA C - Rule 12.12 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.12 Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=39) +---- Thread: MISRA C - Rule 12.12 (/showthread.php?tid=983) |
MISRA C - Rule 12.12 - Akhil - 18-09-2013 Just wanted to know if Line 3 in the following case is a violation for Rule 12.12 ...?? 1. int i; 2. float f=2.1; 3. i=(int)f >>2; //Typecasting should actually take care of the bit implementation of floats Also, If its a violation to Rule 12.12..Does that mean all typecasts (from float to any other type) should also be considered as violationg this rule..? Re: MISRA C - Rule 12.12 - Steve Montgomery - 01-10-2013 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: Code: unsigned int tmp = *(unsigned int *)&f; /* get the float as 32-bit int */ 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. Re: MISRA C - Rule 12.12 - misra-c - 13-12-2013 This example does not violate rule 12.12 as there is no access to the underlying floating representation as discussed by Steve in a previous post. |