Because float
numbers are not precise. You cannot represent every possible value an int
can hold into a float
, even though the maximum value of a float
is much higher.
For instance, run this simple program:
#include <stdio.h>
int main()
{
for(int i = 0; i < 2147483647; i++)
{
float value = i;
int ivalue = value;
if(i != ivalue)
printf("Integer %d is represented as %d in a float\n", i, ivalue);
}
}
You'll quickly see that there are thousands billions of integers that can't be represented as float
s. 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 float
s. Which leaves you roughly with only 100 millions of positive integer that fit correctly into a float
. 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.