Menu

Someone can add FLT_MIN DBL_MIN detection ?

2021-08-15
2021-08-15
  • Daniel Marjamäki

    TBD

     
  • Absol

    Absol - 2021-08-15

    FLT_MIN DBL_MIN - is positive value.
    Many developers use it as minimum (negative) floating point value(like INT_MIN) and it fully wrong and can cause undefined behavior.
    possible to add warn if it found in code?

    Many examples from Valve(thanks for this examples for bad valve developers :) :) )
    Examples: https://github.com/ValveSoftware/source-sdk-2013/issues/519 or https://github.com/perilouswithadollarsign/cstrike15_src/search?q=FLT_MIN

     

    Last edit: Absol 2021-08-15
  • Daniel Marjamäki

    I would like an example code that has a bug .. if possible 4-5 lines of code.. the smaller the better.

    I wonder how do we detect that FLT_MIN is used wrongly? If it cause undefined behavior I think we can warn about that. But if the code just says std::cout << FLT_MIN; or something like that then what is wrong with that?

     

    Last edit: Daniel Marjamäki 2021-08-15
  • Absol

    Absol - 2021-08-15

    You can see examples, and many examples, above )
    or it simple example:

    BAD

    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <cfloat>
    
    float test[3]{0.0,-1.5,-5.5};
    
    int main()
    {
        float minvalue = FLT_MAX;
        float maxvalue = FLT_MIN;
    
        for(int i = 0; i < 3; i++)
        {
            minvalue = std::min(test[i],minvalue);
            maxvalue = std::max(test[i],maxvalue);
        }
    
        std::cout << minvalue << std::endl << maxvalue ;
    
    }
    

    TRUE

    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <cfloat>
    
    float test[3]{0.0,-1.5,-5.5};
    
    int main()
    {
        float minvalue = FLT_MAX;
        float maxvalue = -FLT_MAX;
    
        for(int i = 0; i < 3; i++)
        {
            minvalue = std::min(test[i],minvalue);
            maxvalue = std::max(test[i],maxvalue);
        }
    
        std::cout << minvalue << std::endl << maxvalue ;
    
    }
    

    In this example, vecMax can not be

     
    • Daniel Marjamäki

      I feel a bit skeptic about this. So the heuristic that we should warn about is when a float variable is assigned FLT_MIN and then the variable is compared against some float values in a loop.. and the max value is stored in the variable..

      Even this feels inconclusive for me. If you replace FLT_MIN with 0.0f in your example code I assume no warning should be written. However the code would technically be almost exactly the same. Maybe there are some cases when it is intentional to use FLT_MIN.

       
  • Absol

    Absol - 2021-08-15

    You can use http://cpp.sh/ for test it

     

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.