Re: -Wfloat-equal and comparison to zero

2024-11-15 Thread David Brown via Gcc
On 13/11/2024 22:34, James K. Lowden wrote: On Thu, 14 Nov 2024 10:04:59 +0100 David Brown via Gcc wrote: No. This is - or at least appears to be - missing critical thinking. You are explaining this to someone who designed research databases and who implemented quantitative models that ran

Re: -Wfloat-equal and comparison to zero

2024-11-14 Thread James K. Lowden
On Thu, 14 Nov 2024 10:04:59 +0100 David Brown via Gcc wrote: > No. This is - or at least appears to be - missing critical thinking. You are explaining this to someone who designed research databases and who implemented quantitative models that ran on them. You're entitled to your opinion, of

Re: -Wfloat-equal and comparison to zero

2024-11-14 Thread David Brown via Gcc
On 12/11/2024 22:44, James K. Lowden wrote: On Tue, 12 Nov 2024 18:12:50 +0100 David Brown via Gcc wrote: Under what circumstances would you have code that : ... d) Would be perfectly happy with "x" having the value 2.225e-307 (or perhaps a little larger) and doing the division with that. I

Re: -Wfloat-equal and comparison to zero

2024-11-14 Thread Sad Clouds via Gcc
> It's also not unusual to start with "x" statically initialized to zero, > and use that as an indication to invoke the initialization routine. This is exactly what I have. In my case, if the value remains 0.0 it means the calculations for those metrics are not applicable. The suggestion to u

Re: -Wfloat-equal and comparison to zero

2024-11-13 Thread James K. Lowden
On Tue, 12 Nov 2024 18:12:50 +0100 David Brown via Gcc wrote: > Under what circumstances would you have code that : ... > d) Would be perfectly happy with "x" having the value 2.225e-307 (or > perhaps a little larger) and doing the division with that. > > I think what you really want to check i

Re: -Wfloat-equal and comparison to zero

2024-11-13 Thread James K. Lowden
On Mon, 11 Nov 2024 21:14:43 + (UTC) Joseph Myers via Gcc wrote: > On Sat, 9 Nov 2024, Sad Clouds via Gcc wrote: > > > Even though there is nothing unsafe here and comparison to floating > > point 0.0 value is well defined. > > The point of the warning is that *if you are writing code that

Re: -Wfloat-equal and comparison to zero

2024-11-13 Thread Michael Matz via Gcc
Hello, On Tue, 12 Nov 2024, Thomas Koenig via Gcc wrote: > Am 12.11.24 um 17:25 schrieb Michael Matz via Gcc: > > > If you think of float as > > approximated reals, then yes, division by zero is undefined (not > > somewhat undefined!). > > Depends on how you look at it. > > IEEE 754-2008, for

Re: -Wfloat-equal and comparison to zero

2024-11-12 Thread Michael Matz via Gcc
Hello, On Tue, 12 Nov 2024, Sad Clouds via Gcc wrote: > > I don't think this has anything to do with whether one operand of the > > comparison is a constant. It's still the case when comparing with 0.0 > > that it's OK if your algorithm is designed such that the other operand > > is exact, an

Re: -Wfloat-equal and comparison to zero

2024-11-12 Thread Sad Clouds via Gcc
On Tue, 12 Nov 2024 17:25:14 +0100 (CET) Michael Matz wrote: > Other values for the divisor will also produce Inf. If your goal was to > avoid getting Inf, then checking for equality with zero _won't_ do: Yes you are correct, there are many different ways to overflow and underflow a floating p

Re: -Wfloat-equal and comparison to zero

2024-11-12 Thread Marc Glisse via Gcc
On Tue, 12 Nov 2024, David Brown via Gcc wrote: On 12/11/2024 15:29, Sad Clouds via Gcc wrote: On Mon, 11 Nov 2024 21:14:43 + (UTC) Joseph Myers wrote: I don't think this has anything to do with whether one operand of the comparison is a constant. It's still the case when comparing with

Re: -Wfloat-equal and comparison to zero

2024-11-12 Thread Thomas Koenig via Gcc
Am 12.11.24 um 17:25 schrieb Michael Matz via Gcc: If you think of float as approximated reals, then yes, division by zero is undefined (not somewhat undefined!). Depends on how you look at it. IEEE 754-2008, for example, says in 7.3 "The default result of divideByZero shall be an ∞ correctl

Re: -Wfloat-equal and comparison to zero

2024-11-12 Thread David Brown via Gcc
On 12/11/2024 15:29, Sad Clouds via Gcc wrote: On Mon, 11 Nov 2024 21:14:43 + (UTC) Joseph Myers wrote: I don't think this has anything to do with whether one operand of the comparison is a constant. It's still the case when comparing with 0.0 that it's OK if your algorithm is designed su

Re: -Wfloat-equal and comparison to zero

2024-11-12 Thread Sad Clouds via Gcc
On Mon, 11 Nov 2024 21:14:43 + (UTC) Joseph Myers wrote: > I don't think this has anything to do with whether one operand of the > comparison is a constant. It's still the case when comparing with 0.0 > that it's OK if your algorithm is designed such that the other operand is > exact, and

Re: -Wfloat-equal and comparison to zero

2024-11-11 Thread Joseph Myers via Gcc
On Sat, 9 Nov 2024, Sad Clouds via Gcc wrote: > Even though there is nothing unsafe here and comparison to floating > point 0.0 value is well defined. The point of the warning is that *if you are writing code that thinks of floating-point values as being approximations to real numbers* then such

Re: -Wfloat-equal and comparison to zero

2024-11-10 Thread Jonathan Wakely via Gcc
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, d

Re: -Wfloat-equal and comparison to zero

2024-11-10 Thread Alexander Monakov
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 l

Re: -Wfloat-equal and comparison to zero

2024-11-10 Thread Sad Clouds via Gcc
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

Re: -Wfloat-equal and comparison to zero

2024-11-10 Thread Jonathan Wakely via Gcc
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

Re: -Wfloat-equal and comparison to zero

2024-11-10 Thread Sad Clouds via Gcc
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 not

Re: -Wfloat-equal and comparison to zero

2024-11-09 Thread Andrew Pinski via Gcc
On Sat, Nov 9, 2024, 11:19 AM Sad Clouds via Gcc wrote: > Hello, I don't know if this is a known GCC bug or intentional design, > but code like this: > > double value = 0.0; > ... > if (value == 0.0) > { > ... > } > > Results in the following warning with gcc-12.2.0: > > "... warning: com

-Wfloat-equal and comparison to zero

2024-11-09 Thread Sad Clouds via Gcc
Hello, I don't know if this is a known GCC bug or intentional design, but code like this: double value = 0.0; ... if (value == 0.0) { ... } Results in the following warning with gcc-12.2.0: "... warning: comparing floating-point with ‘==’ or ‘!=’ is unsafe" Even though there is nothing