Page 1 of 1

Rule 13.3 with respect to 0.0

Posted: Tue Jul 13, 2010 5:58 pm
by gs
Does rule 13.3 permit testing a floating point expression against 0.0?

Code: Select all

void f( float flt )
{
if( flt == 0.0 )
    {}
}
If not, how is this test any different than

Code: Select all

void f( float flt )
{
if( !flt )
    {}
}
?

Or does 13.3 not permit the second example either?

Re: Rule 13.3 with respect to 0.0

Posted: Tue Jul 13, 2010 8:09 pm
by William Forbes
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.

Re: Rule 13.3 with respect to 0.0

Posted: Tue Jul 27, 2010 4:54 pm
by misra-c
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).

Re: Rule 13.3 with respect to 0.0

Posted: Thu Jan 30, 2014 3:40 pm
by AstechMA
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)?

Re: Rule 13.3 with respect to 0.0

Posted: Thu Feb 20, 2014 8:42 am
by misra-c
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".