Menu

No longer catching Divide by zero in some cases?

2019-03-18
2019-04-03
  • Steve Hopkins

    Steve Hopkins - 2019-03-18

    Just upgraded from cppcheck v1.33 to v1.78. The code below was formerly flagged as Divide by zero, but is ignored with v1.78.
    float rate = 0
    float range = 0;
    rate= 1.1
    rate = rate/range;

     
  • Daniel Marjamäki

    This one is intentional. One some platforms the result will be INF.

    I think this calculation can crash on some embedded platforms. So if anybody has the time to add a platform configuration we can warn about that properly.

     
  • Steve Hopkins

    Steve Hopkins - 2019-03-19

    FYI, cat /etc/*release displays:
    Red HatEnterprise Linux Client release 5.5 (Tikanga)
    5.4 Redhawk Linux (Lima)

     
  • Daniel Marjamäki

    ok.. so I assume your result will be INF.

    #include <iostream>
    
    int main()
    {
      float rate = 0;
      float range = 0;
      rate= 1.1;
      rate = rate/range;
      std::cout << rate << std::endl;
      return 0;
    }
    

    on my ubuntu machine this program writes "inf" on the screen. Does it write "inf" for you too?

    $ g++ -o 1 1.cpp
    $ ./1
    inf
    

    Here is a platform configuration file: https://github.com/danmar/cppcheck/blob/master/platforms/avr8.xml
    This is where somebody should add the possibility to say that floating point division by zero should be warned about.

     
  • Steve Hopkins

    Steve Hopkins - 2019-04-03

    I ran the test you supplied above, and I got "inf", too.

     

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.