public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105278] New: no warning for precise literals compared with floats
@ 2022-04-14 15:41 dcb314 at hotmail dot com
  2022-04-14 17:51 ` [Bug c++/105278] " egallager at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: dcb314 at hotmail dot com @ 2022-04-14 15:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

            Bug ID: 105278
           Summary: no warning for precise literals compared with floats
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

For this C++ code:

extern void g( int);

void f( float a)
{
        if (a == 0.1234)
                g( 1);
}

recent gcc trunk has nothing to say:

$ /home/dcb/gcc/results/bin/gcc -c -g -O2 -Wall -Wextra -pedantic apr14b.cc
$ 

Here is clang finding the problem:

$ /home/dcb/llvm/results/bin/clang -c -g -O2 -Wall   apr14b.cc
apr14b.cc:6:8: warning: floating-point comparison is always false; constant
cannot be represented exactly in type 'float' [-Wliteral-range]
        if (a == 0.1234)
            ~ ^  ~~~~~~~~~~~
1 warning generated.

Interestingly, if float is replaced by double, the warning goes away.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] no warning for precise literals compared with floats
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
@ 2022-04-14 17:51 ` egallager at gcc dot gnu.org
  2022-04-14 18:44 ` dcb314 at hotmail dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-04-14 17:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org
           Keywords|                            |diagnostic

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
-Wfloat-equal gets you a warning, as does -Wdouble-promotion:

$ /usr/local/bin/gcc -c -g -O2 -Wall -Wextra -pedantic -Wconversion
-Wfloat-equal -Wdouble-promotion 105278.c
105278.c: In function 'f':
105278.c:5:15: warning: comparing floating-point with '==' or '!=' is unsafe
[-Wfloat-equal]
    5 |         if (a == 0.1234)
      |               ^~
105278.c:5:15: warning: implicit conversion from 'float' to 'double' to match
other operand of binary expression [-Wdouble-promotion]
$

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] no warning for precise literals compared with floats
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
  2022-04-14 17:51 ` [Bug c++/105278] " egallager at gcc dot gnu.org
@ 2022-04-14 18:44 ` dcb314 at hotmail dot com
  2022-04-14 19:29 ` egallager at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: dcb314 at hotmail dot com @ 2022-04-14 18:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

--- Comment #2 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to Eric Gallager from comment #1)
> -Wfloat-equal gets you a warning, as does -Wdouble-promotion:

Thanks for that. This looks like another case where an obscure flag
really ought to be in -Wall or at least -Wextra.

I'll be adding it into my local compiler.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] no warning for precise literals compared with floats
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
  2022-04-14 17:51 ` [Bug c++/105278] " egallager at gcc dot gnu.org
  2022-04-14 18:44 ` dcb314 at hotmail dot com
@ 2022-04-14 19:29 ` egallager at gcc dot gnu.org
  2022-04-14 19:43 ` dcb314 at hotmail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-04-14 19:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to David Binderman from comment #2)
> (In reply to Eric Gallager from comment #1)
> > -Wfloat-equal gets you a warning, as does -Wdouble-promotion:
> 
> Thanks for that. This looks like another case where an obscure flag
> really ought to be in -Wall or at least -Wextra.
> 
> I'll be adding it into my local compiler.

Which one of them specifically do you want as part of -Wall or -Wextra? Because
the latter is listed explicitly in some documentation somewhere as an example
of a flag that would be bad to include in -Wall or -Wextra.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] no warning for precise literals compared with floats
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
                   ` (2 preceding siblings ...)
  2022-04-14 19:29 ` egallager at gcc dot gnu.org
@ 2022-04-14 19:43 ` dcb314 at hotmail dot com
  2022-11-16 22:52 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: dcb314 at hotmail dot com @ 2022-04-14 19:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

--- Comment #4 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to Eric Gallager from comment #3)
> (In reply to David Binderman from comment #2)
> > (In reply to Eric Gallager from comment #1)
> > > -Wfloat-equal gets you a warning, as does -Wdouble-promotion:
> > 
> > Thanks for that. This looks like another case where an obscure flag
> > really ought to be in -Wall or at least -Wextra.
> > 
> > I'll be adding it into my local compiler.
> 
> Which one of them specifically do you want as part of -Wall or -Wextra?
> Because the latter is listed explicitly in some documentation somewhere as
> an example of a flag that would be bad to include in -Wall or -Wextra.

Sorry, I should have been more clear -Wfloat-equal. I haven't looked into
the pros and cons of -Wdouble-promotion. 

If you've got some spare time to pin down "some documentation somewhere",
then that would be appreciated.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] no warning for precise literals compared with floats
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
                   ` (3 preceding siblings ...)
  2022-04-14 19:43 ` dcb314 at hotmail dot com
@ 2022-11-16 22:52 ` pinskia at gcc dot gnu.org
  2022-11-16 22:54 ` [Bug c++/105278] -Wliteral-range vs -Wfloat-equal pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-16 22:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note I think GCC's -Wfloat-equal is more reasonible than Clang's
-Wliteral-range really. The reason is because even if something can be
represented exactly in floating point (e.g. 3.0 or even 0.0), you could in
theory still get a few precision mismatch due to FMA or some other slight
differences in HW implementation.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] -Wliteral-range vs -Wfloat-equal
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
                   ` (4 preceding siblings ...)
  2022-11-16 22:52 ` pinskia at gcc dot gnu.org
@ 2022-11-16 22:54 ` pinskia at gcc dot gnu.org
  2022-11-17 13:33 ` egallager at gcc dot gnu.org
  2022-11-17 18:40 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-16 22:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|no warning for precise      |-Wliteral-range vs
                   |literals compared with      |-Wfloat-equal
                   |floats                      |

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I don't think clang implements -Wfloat-equal at all, at least they didn't at
the last time I looked a few years back.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] -Wliteral-range vs -Wfloat-equal
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
                   ` (5 preceding siblings ...)
  2022-11-16 22:54 ` [Bug c++/105278] -Wliteral-range vs -Wfloat-equal pinskia at gcc dot gnu.org
@ 2022-11-17 13:33 ` egallager at gcc dot gnu.org
  2022-11-17 18:40 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-11-17 13:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

--- Comment #7 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #6)
> I don't think clang implements -Wfloat-equal at all, at least they didn't at
> the last time I looked a few years back.

I just checked their diagnostics reference page and it says that they do:
https://clang.llvm.org/docs/DiagnosticsReference.html#wfloat-equal

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/105278] -Wliteral-range vs -Wfloat-equal
  2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
                   ` (6 preceding siblings ...)
  2022-11-17 13:33 ` egallager at gcc dot gnu.org
@ 2022-11-17 18:40 ` pinskia at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-17 18:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105278

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So clang emits one or the other warning for the code but not both.

You can defect the warning in clang by doing:
```
extern void g( int);
void f(float a)
{
  double b = a;
  if (b == 0.1234)
    g( 1);
}
```

LLVM actually opimizes away the comparison while GCC does not.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-11-17 18:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-14 15:41 [Bug c++/105278] New: no warning for precise literals compared with floats dcb314 at hotmail dot com
2022-04-14 17:51 ` [Bug c++/105278] " egallager at gcc dot gnu.org
2022-04-14 18:44 ` dcb314 at hotmail dot com
2022-04-14 19:29 ` egallager at gcc dot gnu.org
2022-04-14 19:43 ` dcb314 at hotmail dot com
2022-11-16 22:52 ` pinskia at gcc dot gnu.org
2022-11-16 22:54 ` [Bug c++/105278] -Wliteral-range vs -Wfloat-equal pinskia at gcc dot gnu.org
2022-11-17 13:33 ` egallager at gcc dot gnu.org
2022-11-17 18:40 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).