Precision of multiplication by 1.0 and int to float conversion

c, c++, floating-point, precision, type-conversion

Solution

No.

If `i` is sufficiently large that `int(float(i)) != i` (assuming float is IEEE-754 single precision, `i = 0x1000001` suffices to exhibit this) then this is false, because multiplication by `1.0f` forces a conversion to `float`, which changes the value even though the subsequent multiplication does not.

However, if `i` is a 32-bit integer and `double` is IEEE-754 double, then it is true that `int(i*1.0) == i`.

Just to be totally clear, multiplication by `1.0f` is exact. It's the conversion from `int` to `float` that may not be.

Problem

Is it safe to assume that the condition `(int)(i * 1.0f) == i` is true for any integer `i`?

Original source