# V1064. The left operand of integer division is less than the right one. The result will always be zero.

The analyzer has detected a suspicious expression that contains an integer division operation, with the left operand always being less than the right operand. Such an expression will always evaluate to zero.

Consider the following example:

    if ( nTotal > 30 && pBadSource->m_nNegativeVotes / nTotal > 2/3 )
    {
      ....
    }

Since both literals '2' and '3' are of integer type, the quotient will also be integer and, therefore, zero. It means the expression above is equivalent to the following one:

    if ( nTotal > 30 && pBadSource->m_nNegativeVotes / nTotal > 0 )
    {
      ....
    }

A correct way to fix this error is to explicitly cast one of the operands to a floating-point type, for example:

    if ( nTotal > 30 && pBadSource->m_nNegativeVotes / nTotal >
                                static_cast<float>(2)/3 )
    {
      ....
    }

Or:

    if ( nTotal > 30 && pBadSource->m_nNegativeVotes / nTotal > 2.0f/3 )
    {
      ....
    }

The analyzer also issues a warning, if it detects a suspicious expression that contains the modulo operation, with the dividend always being less than the divisor. Such an expression will always evaluate to the dividend value.

Let's take a look at the following example:

    void foo()
    {
      unsigned int r = 12;
      const unsigned int r3a = (16 + 5 - r) % 16;
    }

Here the expression '16+5-r' evaluates to 9. This value is less than the divisor '16'. Therefore, the modulo operation, in this case, does not make sense. The result will be 9.

Consider a more complex example:

    int get_a(bool cond)
    {
      return cond ? 3 : 5;
    }

    int get_b(bool cond)
    {
      return cond ? 7 : 9;
    }

    int calc(bool cond1, bool cond2)
    {
      return get_a(cond1) % get_b(cond2);
    }

The 'calc' function contains the modulo operation. The dividend receives the values 3 or 5. The divisor receives the values 7 or 9. Thus, there are four variants to evaluate the modulo operation: '3 % 7', '5 % 7', '3 % 9', '5 % 9'. In each variant, the dividend is less than the divisor. So, the operation is meaningless.

If the analyzer has issued a warning on your code, we recommend checking this code fragment for logical errors. Perhaps one of the operands of the modulo operation occurs in an unexpected way. Or perhaps another operation must be used instead of the modulo operation.

This diagnostic is classified as:

CWE-682

You can look at examples of errors detected by the V1064 diagnostic.