I have code that performs flushing of subnormal 32-bit floats to plus or minus zeroes.
staticinlinefloatflush(floatx) {
if(fabs(x)<FLT_MIN) {
return(x>=0.0) ? 0.0 : -0.0; // reports "Same value in both branches of ternary operator"
} elsereturnx;
}
Cppcheck reports "Same value in both branches of ternary operator." . It considers 0.0 and -0.0 to be equal.
But they are not. E.g. the following program compiled with GCC returns with 1, meaning the comparison return non-equality.
$cppcheck--enable=all--platform=native-D__GNUC__-D__linux__-D__x86_64__pmzero.cCheckingpmzero.c...Checkingpmzero.c:__GNUC__=1;__linux__=1;__x86_64__=1...pmzero.c:6:41:style:Samevalueinbothbranchesofternaryoperator.[duplicateValueTernary]return(x>=0.0)?0.0:-0.0;//reports"Same value in both branches of ternary operator"^
The input file:
$ cat pmzero.c
#include <math.h>#include <float.h>
static inline float flush(float x){if(fabs(x) < FLT_MIN){return(x >=0.0) ? 0.0 : -0.0; // reports "Same value in both branches of ternary operator"}elsereturn x;}
Last edit: Grigory Rechistov 2022-10-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have code that performs flushing of subnormal 32-bit floats to plus or minus zeroes.
Cppcheck reports "Same value in both branches of ternary operator." . It considers 0.0 and -0.0 to be equal.
But they are not. E.g. the following program compiled with GCC returns with 1, meaning the comparison return non-equality.
I am using Cppcheck 2.7 dev built from SHA 31ea13eb0cf396c6b7ff66948808999c74f60f36 at 4th of February.
Thank you!
This is how invoke the analysis:
The input file:
Last edit: Grigory Rechistov 2022-10-26
Also reproduced if I use Cppcheck built from the yesterday's code snapshot (SHA 8672e12a7a5173c0322b2f3341786e6b370afdde)
Hmmm, I wonder how well cppcheck handles other special values in IEEE854.