public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103531] New: Prpose compiler warning when ceil functions used on integral value
@ 2021-12-02 13:51 eyalroz1 at gmx dot com
  2021-12-02 14:06 ` [Bug c/103531] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: eyalroz1 at gmx dot com @ 2021-12-02 13:51 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103531
           Summary: Prpose compiler warning when ceil functions used on
                    integral value
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eyalroz1 at gmx dot com
  Target Milestone: ---

The ceil functions (ceil() and ceilf() in C) take floating-point arguments:
double or float.

If the programmer passes an integer value to one of these functions, it'll get
converted to float/double, and the function will be idempotent. It is thus
typically useless to do so, but also harmless in itself.

But this is _not_ harmless when the programmer assumes they are running ceil()
on what they believe is a floating-point value, but actually isn't. A "typical"
example:

forgot_whether_this_type_is_integral_t x = get_x();
int covers_half = ceil(x / 2); 

If x is a floating-point value, this codes will act as expected; but if it's
integral, it will actually place the _floor_ of x / 2.0 in covers_half.

I propose, therefore, that invoking ceil()/ceilf() on an integral value trigger
a warning. Alternatively, that some more complicated conditions trigger the
warning, e.g. invoking ceil() on the result of an integer division or integer
multiplication.

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

* [Bug c/103531] Prpose compiler warning when ceil functions used on integral value
  2021-12-02 13:51 [Bug c/103531] New: Prpose compiler warning when ceil functions used on integral value eyalroz1 at gmx dot com
@ 2021-12-02 14:06 ` rguenth at gcc dot gnu.org
  2021-12-02 19:50 ` [Bug c/103531] Propose compiler warning when ceil/ceilf " eyalroz1 at gmx dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-12-02 14:06 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
     Ever confirmed|0                           |1
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-12-02

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  Also applies to the C++ frontend.

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

* [Bug c/103531] Propose compiler warning when ceil/ceilf used on integral value
  2021-12-02 13:51 [Bug c/103531] New: Prpose compiler warning when ceil functions used on integral value eyalroz1 at gmx dot com
  2021-12-02 14:06 ` [Bug c/103531] " rguenth at gcc dot gnu.org
@ 2021-12-02 19:50 ` eyalroz1 at gmx dot com
  2021-12-03  4:03 ` egallager at gcc dot gnu.org
  2021-12-03  7:37 ` eyalroz1 at gmx dot com
  3 siblings, 0 replies; 5+ messages in thread
From: eyalroz1 at gmx dot com @ 2021-12-02 19:50 UTC (permalink / raw)
  To: gcc-bugs

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

Eyal Rozenberg <eyalroz1 at gmx dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Prpose compiler warning     |Propose compiler warning
                   |when ceil functions used on |when ceil/ceilf used on
                   |integral value              |integral value

--- Comment #2 from Eyal Rozenberg <eyalroz1 at gmx dot com> ---
This also applies to C++, although in C++ you might be doing this in a
templated context, e.g. 

T x = get_x();
auto y = ceil(x); 

which is fine for floating-point T's and fishy for integral T's.

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

* [Bug c/103531] Propose compiler warning when ceil/ceilf used on integral value
  2021-12-02 13:51 [Bug c/103531] New: Prpose compiler warning when ceil functions used on integral value eyalroz1 at gmx dot com
  2021-12-02 14:06 ` [Bug c/103531] " rguenth at gcc dot gnu.org
  2021-12-02 19:50 ` [Bug c/103531] Propose compiler warning when ceil/ceilf " eyalroz1 at gmx dot com
@ 2021-12-03  4:03 ` egallager at gcc dot gnu.org
  2021-12-03  7:37 ` eyalroz1 at gmx dot com
  3 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2021-12-03  4:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org
             Blocks|                            |87403

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
-Wtraditional-conversion catches this:

$ /usr/local/bin/gcc -c -Wall -Wextra -pedantic -Wconversion -Wdouble-promotion
-Wtraditional-conversion 103531.c
103531.c: In function 'foo':
103531.c:5:30: warning: passing argument 1 of 'ceil' as floating rather than
integer due to prototype [-Wtraditional-conversion]
    5 |     int covers_half = ceil(x / 2);
      |                            ~~^~~
103531.c:5:23: warning: conversion from 'double' to 'int' may change value
[-Wfloat-conversion]
    5 |     int covers_half = ceil(x / 2);
      |                       ^~~~
$


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87403
[Bug 87403] [Meta-bug] Issues that suggest a new warning

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

* [Bug c/103531] Propose compiler warning when ceil/ceilf used on integral value
  2021-12-02 13:51 [Bug c/103531] New: Prpose compiler warning when ceil functions used on integral value eyalroz1 at gmx dot com
                   ` (2 preceding siblings ...)
  2021-12-03  4:03 ` egallager at gcc dot gnu.org
@ 2021-12-03  7:37 ` eyalroz1 at gmx dot com
  3 siblings, 0 replies; 5+ messages in thread
From: eyalroz1 at gmx dot com @ 2021-12-03  7:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Eyal Rozenberg <eyalroz1 at gmx dot com> ---
(In reply to Eric Gallager from comment #3)
> -Wtraditional-conversion catches this:

Well... you're technically right, but:

1. That is a much wider warning. If someone were to turn this on they would get
a gazillion warnings which would drown out this one.
2. (Weak point) Few people would consider turning this on given the
description.
3. This doesn't work for C++.

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

end of thread, other threads:[~2021-12-03  7:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-02 13:51 [Bug c/103531] New: Prpose compiler warning when ceil functions used on integral value eyalroz1 at gmx dot com
2021-12-02 14:06 ` [Bug c/103531] " rguenth at gcc dot gnu.org
2021-12-02 19:50 ` [Bug c/103531] Propose compiler warning when ceil/ceilf " eyalroz1 at gmx dot com
2021-12-03  4:03 ` egallager at gcc dot gnu.org
2021-12-03  7:37 ` eyalroz1 at gmx dot com

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).