Re: -Wfloat-equal and comparison to zero
On Sun, 10 Nov 2024, 08:26 Sad Clouds via Gcc, wrote: > On Sat, 9 Nov 2024 11:49:56 -0800 > Andrew Pinski wrote: > > > You can use the diagnostic pragma to disable it directly for the > statement. > > Thanks for the tip. After a quick search, I came across this page, > which explains it: > https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html > > > But I will note comparing double even against 0 can be problematic. You > > might not get exactly 0 in some cases with things like FMA or when using > > x87 instructions set. > > I'm not sure I understand this. As far as I know, 0.0 can be > represented exactly in a floating point type. So the statement > "if (value == 0.0)" should always be true if value is exactly 0.0. > > I'm not really concerned about "value = 0.1 - 0.1" not resulting in 0.0. > That will result in 0.0 though. But 1 - (10 * 0.1) won't, and so the warning is pointing out that any exact equality comparisons can be affected by this kind of problem. If you don't like the warning, don't enable it. It is more of a case where value was explicitly initialised to 0.0 and > then later used in the equality comparison. > Does clang only have a special case for 0.0, or for any literal value? >
Re: -Wfloat-equal and comparison to zero
On Sun, 10 Nov 2024 09:45:41 + Jonathan Wakely wrote: > Does clang only have a special case for 0.0, or for any literal value? > It looks like clang can detect which floating point literals can be represented exactly and does not generate any warnings for those. $ cat test.c #include int main(void) { double value = 0.0; if (value == 0.0) { printf("value == 0.0\n"); } if (value == 1.0) { printf("value == 1.0\n"); } if (value == 1.1) { printf("value == 1.1\n"); } } $ clang -Wfloat-equal test.c test.c:17:12: warning: comparing floating point with == or != is unsafe [-Wfloat-equal] if (value == 1.1) ~ ^ ~~~ 1 warning generated. As suggested by Andrew, I used GCC pragmas to silence the warnings for specific lines of code. So I get the benefit of -Wfloat-equal checks without the spurious cases. Thanks.
Re: -Wfloat-equal and comparison to zero
On Sun, 10 Nov 2024, Jonathan Wakely via Gcc wrote: > But 1 - (10 * 0.1) won't, and so the warning is pointing out that any exact > equality comparisons can be affected by this kind of problem. If you don't > like the warning, don't enable it. I think OP's questions are in good faith and your last statement is unnecesarily passive-aggressive. If we respond to queries like that, it just reflects badly on us, because the implication is that we are not interested in criticisms for diagnostics or clarifying their behavior. Our warning is extremely simplistic, it warns even for 'x != x', and even the wording of the warning is bad: it says '... is unsafe' but there are no safety concerns here. I can see situations where warning for floating-point comparison makes sense even by default: float x; ... if (x != 0.1) // not 0.1f since 0.1f != 0.1 this could yield something like warning: comparison is always true but warning on every comparison is indefensible. > Does clang only have a special case for 0.0, or for any literal value? Their behavior is not documented, and it is not completely straightforward to me looking at the source, but they do not issue the warning if the value is exactly representable in the format (but they warn if it was implicitly cast from an integer, like in 'x == 0', which is probably a bug). They also suppress the warning in some other cases, e.g. for 'x == x'. Alexander
Re: -Wfloat-equal and comparison to zero
On Sun, 10 Nov 2024, 11:13 Alexander Monakov, wrote: > > On Sun, 10 Nov 2024, Jonathan Wakely via Gcc wrote: > > > But 1 - (10 * 0.1) won't, and so the warning is pointing out that any > exact > > equality comparisons can be affected by this kind of problem. If you > don't > > like the warning, don't enable it. > > I think OP's questions are in good faith and your last statement is > unnecesarily > passive-aggressive. If we respond to queries like that, it just reflects > badly > on us, because the implication is that we are not interested in criticisms > for > diagnostics or clarifying their behavior. > That's fair, I apologise for my tone. My point is that the warning is not enabled by -Wall and is behaving as documented in the manual, so if the warning isn't useful to you, there's no need to enable it. > Our warning is extremely simplistic, it warns even for 'x != x', and even > the > wording of the warning is bad: it says '... is unsafe' but there are no > safety > concerns here. > Agreed on both points. > I can see situations where warning for floating-point comparison makes > sense > even by default: > > float x; > ... > if (x != 0.1) // not 0.1f > > since 0.1f != 0.1 this could yield something like > > warning: comparison is always true > > but warning on every comparison is indefensible. > > > Does clang only have a special case for 0.0, or for any literal value? > > Their behavior is not documented, and it is not completely straightforward > to me > looking at the source, but they do not issue the warning if the value is > exactly > representable in the format (but they warn if it was implicitly cast from > an > integer, like in 'x == 0', which is probably a bug). They also suppress the > warning in some other cases, e.g. for 'x == x'. > > Alexander >
Check mail
Re: -Wfloat-equal and comparison to zero
On Sat, 9 Nov 2024 11:49:56 -0800 Andrew Pinski wrote: > You can use the diagnostic pragma to disable it directly for the statement. Thanks for the tip. After a quick search, I came across this page, which explains it: https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html > But I will note comparing double even against 0 can be problematic. You > might not get exactly 0 in some cases with things like FMA or when using > x87 instructions set. I'm not sure I understand this. As far as I know, 0.0 can be represented exactly in a floating point type. So the statement "if (value == 0.0)" should always be true if value is exactly 0.0. I'm not really concerned about "value = 0.1 - 0.1" not resulting in 0.0. It is more of a case where value was explicitly initialised to 0.0 and then later used in the equality comparison. Thanks.
gcc-15-20241110 is now available
Snapshot gcc-15-20241110 is now available on https://gcc.gnu.org/pub/gcc/snapshots/15-20241110/ and on various mirrors, see https://gcc.gnu.org/mirrors.html for details. This snapshot has been generated from the GCC 15 git branch with the following options: git://gcc.gnu.org/git/gcc.git branch master revision 32cf28ccc9e77ce0e21db38fa1bdfe1b71bbd031 You'll find: gcc-15-20241110.tar.xz Complete GCC SHA256=09d8da5ca6dd5cc5cfbbb620a6e334f77f7d871599aaf5fa9a46657e7110f7bb SHA1=b163d70c3fe7bf284170a6c8227e7748237ea62e Diffs from 15-20241103 are available in the diffs/ subdirectory. When a particular snapshot is ready for public consumption the LATEST-15 link is updated and a message is sent to the gcc list. Please do not use a snapshot before it has been announced that way.