MISRA Discussion Forums
Rule 6-2-2 Usage of std::fpclassify - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.6 Statements (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=135)
+---- Thread: Rule 6-2-2 Usage of std::fpclassify (/showthread.php?tid=1786)



Rule 6-2-2 Usage of std::fpclassify - cgpzs - 21-09-2026

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.


RE: Rule 6-2-2 Usage of std::fpclassify - misra cpp - 25-09-2026

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.