This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/41779] New: Spurious integral promotion


The following code produces a warning about precision loss in an int-to-float
conversion:

    $cat warn.cpp
    float f(float x, unsigned short y)
    {
        return x * y;
    }
    $g++ -Wconversion -c -o warn.o warn.cpp
    warn.cpp: In function ?float f(float, short unsigned int)?:
    warn.cpp:3: warning: conversion to ?float? from ?int? may alter its value
    $

The code snippet does not have an int-to-float conversion.

It does have an unsigned short-to-float conversion according to the C++
standard, chapter 5 [expr] para 9: "[inapplicable note on long double and
double...] Otherwise, if either operand is float, the other shall be converted
to float [...]". Only after this rule, integral promotions are performed, which
could promote the unsigned short to an int.

Moreover, he conversion does not lose information since the 16 bits of the
unsigned short fit amply in the float's mantissa.

Not tested with other shorter-than-int types. This problem might apply there as
well...


-- 
           Summary: Spurious integral promotion
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: zweije at xs4all dot nl
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41779


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]