RE: how to avoid -Wconversion warning about float to integer conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I just found your email to the gcc-help list by chance. The new
-Wconversion was mostly implemented by me and in your case it is not
warning about float to int conversion (your code has an explicit cast
which will suppress such warning).

Your code:

   wxSize& Scale(float xscale, float yscale)
        { x = (int)(x*xscale); y = (int)(y*yscale); return *this; }

(I assume that both x and y are int)

and the warning by -Wconversion:

warning: conversion to ‘float’ from ‘int’ may alter its value

Read it again. Conversion to 'float' from 'int', that is, an integer
converted to float type. It is warning that if you convert an int to
float and back to int, you may not get the same value. In your code,
x*scale converts x to float and this conversion is not exact for the
whole range of values of int. One example, try with float f =
16777217; int x = f; and print x

In your case, you may not care about this loss of precision, so you
could always use an explicit cast to (float) to avoid the warning.

BTW, ((int) 0.99999) == 0, so be sure that is what you want to do.

BTW2, not in this case, but just to mention that Wconversion was
implemented very recently (just 2 releases ago), so there are bugs,
and some of them may be fixed in the latest release of GCC.

More info: http://gcc.gnu.org/wiki/NewWconversion

Cheers,

Manuel.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux