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.

Reply via email to