Converting unsigned long long to float and losing precision

3.8k views Asked by At

I have two unsigned long long numbers l1 and l2. I cast those numbers to float and save them as f1 and f2.

f1 = (float) l1;
f2 = (float) l2;

Now consider l1 > l2 by some arbitrary amount, say 100.

Can there be a case such that f1 < f2 ?. (Here f1 is converted from l1 which is greater than l2). If yes, can you please demonstrate how to find such a number?

sizeof(float) = 4 and sizeof(unsigned long long) = 8.
1

There are 1 answers

4
Pascal Cuoq On BEST ANSWER

Can there be a case such that f1 < f2

No: For any rounding mode, the conversion from integer to float is increasing. As long as you do not change the rounding mode between the conversion of l1 and that of l2, f1 and f2 are in the same order as l1 and l2 (they may be equal where l1 and l2 were different, though).