Posts: 29
Threads: 3
Joined: Dec 2008
Reputation:
0
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>
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
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
Posts: 1
Threads: 0
Joined: Jun 2013
Reputation:
0
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>
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
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