This is the mail archive of the gcc@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]

Re: Warning for converting (possibly) negative float/double to unsigned int


On 02/26/2016 09:28 PM, Bradley Lucier wrote:
Perhaps this question is appropriate for the gcc mail list.

Converting a float/double to unsigned int is undefined if the result would be
negative when converted to a signed int.

x86-64 and arm treat this condition differently---x86-64 returns a value whose
bit pattern is the same as the bit pattern for converting to signed int, and
arm returns zero.  So it would be nice to have a warning that this will (or
could) happen.

I couldn't find such a warning in the GCC manual or in the GCC code base.

prog.cc:4:23: warning: overflow in implicit constant conversion [-Woverflow]
     unsigned int z = -2.0;
                       ^~~
prog.cc:5:12: warning: conversion to 'unsigned int' from 'double' may alter its value [-Wfloat-conversion]
     return x;
            ^

Both warnings exist since quite a long time (10 years?), the latter under the more general -Wconversion:
https://gcc.gnu.org/wiki/NewWconversion#Frequently_Asked_Questions

Cheers,

	Manuel.


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