This is the mail archive of the gcc-help@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: how to avoid -Wconversion warning about float to integer conversion


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]