Rule 13.3 with respect to 0.0

6.13 Control Statement Expressions

Moderators: misra-c, david ward

gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Rule 13.3 with respect to 0.0

Postby gs » Tue Jul 13, 2010 5:58 pm

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?

William Forbes
Posts: 29
Joined: Wed Dec 17, 2008 9:48 pm
Company: TRW Automotive

Re: Rule 13.3 with respect to 0.0

Postby William Forbes » Tue Jul 13, 2010 8:09 pm

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.

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 13.3 with respect to 0.0

Postby misra-c » Tue Jul 27, 2010 4:54 pm

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

AstechMA
Posts: 1
Joined: Wed Jun 26, 2013 3:07 pm
Company: Automotive Safety Technologies GmbH

Re: Rule 13.3 with respect to 0.0

Postby AstechMA » Thu Jan 30, 2014 3:40 pm

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)?

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 13.3 with respect to 0.0

Postby misra-c » Thu Feb 20, 2014 8:42 am

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


Return to “6.13 Control Statement Expressions”

Who is online

Users browsing this forum: No registered users and 1 guest