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?
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When this check is added I think a careful investigation will be needed to see that the noise ratio is acceptable. I don't want to warn about some FLT_MIN pattern if that is used by intention in various places.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think 'if DBL_MAX and DBL_MIN found in same code block, write warn about DBL_MIN is 'Minimum representable positive floating-point number.' (or FLT_MIN)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
TBD
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
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
You can see examples, and many examples, above )
or it simple example:
BAD
TRUE
In this example, vecMax can not be
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
with0.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.I have created https://trac.cppcheck.net/ticket/10411
When this check is added I think a careful investigation will be needed to see that the noise ratio is acceptable. I don't want to warn about some FLT_MIN pattern if that is used by intention in various places.
I think 'if DBL_MAX and DBL_MIN found in same code block, write warn about DBL_MIN is 'Minimum representable positive floating-point number.' (or FLT_MIN)
Thanks! I mentioned that heuristic in the ticket.
You can see examples above in valve repositories (This error happens very often with them)
https://github.com/ValveSoftware/source-sdk-2013/issues/519
https://github.com/perilouswithadollarsign/cstrike15_src/search?q=FLT_MIN
https://www.cplusplus.com/reference/cfloat/
https://stackoverflow.com/questions/7973737/why-are-flt-max-and-flt-min-not-positive-and-negative-infinity-and-what-is-thei
https://stackoverflow.com/questions/2528039/why-is-flt-min-equal-to-zero
You can use http://cpp.sh/ for test it