Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 13.3 with respect to 0.0
#1
Does rule 13.3 permit testing a floating point expression against 0.0?
Code:
void f( float flt )
{
if( flt == 0.0 )
    {}
}

If not, how is this test any different than
Code:
void f( float flt )
{
if( !flt )
    {}
}
?

Or does 13.3 not permit the second example either?
Reply
#2
Rule 13.3 actually contains an example showing that comparison to 0.0F is forbidden.
In my opinion the ! operator breaks this rule as it test for equality to 0.
<t></t>
Reply
#3
Rule 13.3 does not permit testing a floating point expression against 0.0. As pointed out by William Forbes, there is an example of this non-compliant code fragment in the rule text.

The C Standard defines !E to be equivalent to (0 == E). Therefore, application of ! to an expression of floating point type is breaks the rule because it is equivalent to an equality comparison against 0.

In the expression (0 == E), if E has a floating-point type 0 would be implicitly converted from int to the floating-point type. Therefore, !E would break the spirit of Rule 10.1 (implicit conversions from an integer type) even though not directly breaking the rule.

The expression !E, where E has floating-point type, would also break Rule 12.6 (operands of logical operators should be effectively Boolean).
Posted by and on behalf of the MISRA C Working Group
Reply
#4
Besides memory usage and the fact that it would be "farer away" from flag coding history, what aspect or rule of ISO 9899:1990 or MISRA-C:2004 forbids me to typedef a double to my projects effective boolean type, with TRUE equivalent to 1.0 and FALSE equivalent to 0.0?

You said !E is evquivalent to (E == 0.0).
Does this mean that (!3.7) returns an integer with value 0 (as all comparisons AFAIK return int)?
<t></t>
Reply
#5
There is nothing in the MISRA-C:2004 rules that prevents your project defining an "effectively boolean type" as a double. However, the MISRA group would strongly recommend that you did not do this.

You are correct in saying that !3.7 will deliver an integer value of 0. However, for the purposes of some of the MISRA rules, the "!" is considered to return the MISRA "effectively boolean type".
Posted by and on behalf of the MISRA C Working Group
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)