Main Content

MISRA C:2012 Rule 21.12

The standard header file <fenv.h> shall not be used

Description

Rule Definition

The standard header file <fenv.h> shall not be used.

Rationale

In some cases, the values of the floating-point status flags are unspecified. Attempts to access them can cause undefined behavior.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <fenv.h>     /* Non-compliant */

void func(float x, float y) {
    float z;

    feclearexcept(FE_DIVBYZERO);              /* Non-compliant */ 
    z = x/y;

    if(fetestexcept (FE_DIVBYZERO))  {        /* Non-compliant */ 
    }
    else {
#pragma STDC FENV_ACCESS ON
        z=x*y;
        if(z>x) {
#pragma STDC FENV_ACCESS OFF
            if(fetestexcept (FE_OVERFLOW)) {  /* Non-compliant */ 
            }
        }
    }
}

In this example, the rule is violated when the identifiers feclearexcept and fetestexcept, and the macros FE_DIVBYZERO and FE_OVERFLOW are used.

Check Information

Group: Standard libraries
Category: Required
AGC Category: Required

Version History

Introduced in R2015b

expand all