Menu

Check for integer rounding?

2015-02-14
2015-02-22
  • AnotherUser

    AnotherUser - 2015-02-14

    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?

    {
    
        int i,j,k;
        i = 9;
        j = 10;
        k = 5;
    
        cerr << (i-j)/k << endl;
    
    }
    
     
  • Daniel Marjamäki

    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?

     
  • AnotherUser

    AnotherUser - 2015-02-22

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.