I recently had an integer rounding bug, where the result was always zero, as floating point should have been used for the intermediate calculation involving non-constant variables. Cppcheck (git 524828a...) did not warn me about this. Is this implemented?
{
int i,j,k;
i = 9;
j = 10;
k = 5;
cerr << (i-j)/k << endl;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
how could we see that the truncation is unintentional?
I didn't show it in my example, but in my case (and in the comment), all the variables were const - it is highly unlikely that a zero value would be expected from a const calculation where all the values are obtained from local variables.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I recently had an integer rounding bug, where the result was always zero, as floating point should have been used for the intermediate calculation involving non-constant variables. Cppcheck (git 524828a...) did not warn me about this. Is this implemented?
there is no warning for such code.
people often use integer division by intention.
we don't want to warn when the result is truncated by intention. that would be a false positive.
how could we see that the truncation is unintentional?
I didn't show it in my example, but in my case (and in the comment), all the variables were const - it is highly unlikely that a zero value would be expected from a const calculation where all the values are obtained from local variables.