If I have an int, convert it to a double, then convert the double back to an int, am I guaranteed to get the same value back that I started with? In other words, given this function:
int passThroughDouble(int input)
{
double d = input;
return d;
}
Am I guaranteed that passThroughDouble(x) == x for all ints x?
No it isn’t. The standard says nothing about the relative sizes of
intanddouble.If
intis a 64-bit integer anddoubleis the standard IEEE double-precision, then it will already fail for numbers bigger than2^53.That said,
intis still 32-bit on the majority of environments today. So it will still hold in many cases.