Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 6-2-2 Usage of std::fpclassify
#1
Hi,

We have seen a creative way to work around MISRA Rule 6-2-2:

Code:
float x = ...;
float y = ...;

if (x == 0.F)  // non-compliant
if (x == y)    // non-compliant

if (std::fpclassify(x)     == FP_ZERO)  // compliant?
if (std::fpclassify(x - y) == FP_ZERO)  // compliant?

Would the last 2 examples be compliant with the rule?

We argue that this is an indirect test for equality and thus non-compliant.
Reply
#2
The short unhelpful answer is that this code is not covered by MISRA C++:2008, as fpclassify didn't come in until C++:2011.

The more considered answer is that the use of fpclassify is technically legal, but certainly wasn't considered when we wrote the rule, and ought to be regarded as malicious compliance and highlighted during code review - as should any attempt to bypass a rule in a way that obfuscates the code's meaning.

It would be better to deviate an obvious comparison with zero, if there is a good reason why such a test is appropriate, e.g. identifying a floating point parameter that if not required and which has been explicitly set to zero. See the MISRA compliance document.
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)