1

This is my first time asking a question, and my English is not good! When a Floating-point arithmetic number (size comparison) appears in my if statement, an error is reported that triggers MISRA R13.5. This is my code section:

static float32_t g_A_pf[4]={0.1f};
static float32_t g_A_f = 0.00001f;
static float32_t g_B_f = 0.1f;
static float32_t g_C_f = 0.1f;
static float32_t g_D_f = 0.1f;
        /* Float */
    if( ( g_A_pf[0]  > g_A_pf[1]  ) && (g_A_pf[2]  < g_A_pf[3]  ) )
        {
                ;/* MISRA 2012 Rule 13.5: right hand operand of && or || operator may not contain side effects  */
        }

    if( ( g_A_f   > g_B_f   ) && (g_C_f   < g_D_f   ) )
        {
                ;/* MISRA 2012 Rule 13.5: right hand operand of && or || operator may not contain side effects  */
        }

    if( ( t_A_f   > t_B_f   ) && (t_C_f   < t_D_f   ) )
        {
                ;/* MISRA 2012 Rule 13.5: right hand operand of && or || operator may not contain side effects  */
    }
the busybee
  • 10,755
  • 3
  • 13
  • 30
Shopping
  • 11
  • 2
  • 2
    That error doesn't make sense. Accessing an array element and inequality operators don't have side effects. – Barmar Jun 08 '23 at 03:53
  • How are you compiling the code? No compiler I know about gives MISRA warnings *by default* – Daniel H Jun 08 '23 at 03:58
  • The compiler I am using is GreenHills. If the error is meaningless, can I assume that I can set the option to turn it off in the software? – Shopping Jun 08 '23 at 03:59
  • I tested three different data types, uint32_ T will not report an error – Shopping Jun 08 '23 at 04:07
  • This looks like a bug in the warning generator to me. There are potential issues with comparing floating point values, because they're not precise (`==` comparisons are particularly problematic), but it has nothing to do with side effects. – Barmar Jun 08 '23 at 04:30
  • 4
    I wonder if this could have to do with the way `float32_t` is defined. What happens if you use native `float`? – nielsen Jun 08 '23 at 07:47
  • @nielsen Not only that, the relevant standard for MISRA C 2012 would almost certainly be C11 (or perhaps C99). The [(draft) C11 standard is readily available online](https://port70.net/~nsz/c/c11/n1570.html), and it has no `float32_t` type. Neither does [the C99 standard](https://port70.net/~nsz/c/c99/n1256.html). So what is `float32_t` here? If `float32_t` is a locally-defined type, that's a dangerous choice of name. It *might* be a reserved identifier under the C standard, it's *certainly* a reserved identifier under POSIX. – Andrew Henle Jun 08 '23 at 12:55
  • (cont) [Proposals to add types such as `float32_t` to the C and C++ standards have existed for at least a decade](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1703.pdf), and Boost provides a `float32_t` type already. The chances of a future collision are substantial. – Andrew Henle Jun 08 '23 at 12:56
  • 3
    I believe that this is unintended: https://forum.misra.org.uk/thread-1597.html – ChrisBD Jun 08 '23 at 12:58

0 Answers0