I assume that by float you mean a 32-bit binary value of an IEEE-754 floating point, by double you mean a 64-bit binary value of an IEEE-754 floating point, and by int you mean a 32-bit integer.
Why is this happening? The float range far exceeds the int range.
Yes, but the precision of the float is only 7-9 decimal digits. To be more specific, the value is only 24 bits wide ... so if you try to store 32 bits of information there, you will have problems.
but when converting from int to double there is no change in value
Of course, since double has a 53-bit value - enough space for a 32-bit integer!
To think of it differently, the gap between consecutive int values ββis always 1 ... whereas the gap between consecutive float values ββstarts very, very small ... but increases as the value increases, it reaches "more than 2", until you reach the limit of int ... so you get on a scene where not every int can be accurately represented.
To think of it differently, you can simply use the principle of pigeon-holes ... even ignoring the NaN values, there can be no more than 2 32 float , and at least one of them is not an exact int value - take 0, 5, for example. There are 2 values ββof 32 int , so at least one int value does not have an exact representation of float .
source share