If we restrict consideration to the "traditional" IEEE-754-style representation of floating-point types, then you can expect this conversion to be value-preserving if and only if the mantissa of the type double
has as many bits as there are non-sign bits in type int
.
Mantissa of a classic IEEE-754 double
type is 53-bit wide (including the "implied" leading bit), which means that you can represent integers in [-2^53, +2^53]
range precisely. Everything out of this range will generally lose precision.
So, it all depends on how wide your int
is compared to your double
. The answer depends on the specific platform. With 32-bit int
and IEEE-754 double
the equality should hold.
assert(std::numeric_limits<int>::digits <= std::numeric_limits<double>::digits);
- en.cppreference.com/w/cpp/types/numeric_limits/digitsstatic_assert
. :Pstatic_assert(std::numeric_limits<int>::digits <= std::numeric_limits<double>::digits, "barf");