I am getting a benign warning about possible data loss
warning C4244: ‘argument’ : conversion from ‘const int’ to ‘float’, possible loss of data
Question
I remember as if float has a larger precision than int. So how can data be lost if I convert from a smaller data type (int) to a larger data type (float)?
Because
floatnumbers are not precise. You cannot represent every possible value anintcan hold into afloat, even though the maximum value of afloatis much higher.For instance, run this simple program:
You’ll quickly see that there are
thousandsbillions of integers that can’t be represented asfloats. For instance, all integers between the range 16,777,219 and 16,777,221 are represented as 16,777,220.EDIT again Running that program above indicates that there are 2,071,986,175 positive integers that cannot be represented precisely as
floats. Which leaves you roughly with only 100 millions of positive integer that fit correctly into afloat. This means only one integer out of 21 is right when you put it into a float.I expect the numbers to be the same for the negative integers.