Does float convert to double and back to float, gives the same value in C ++

Suppose in the following code

float f1 = ...; double d1 = static_cast<double>(f1); float f2 = static_cast<float>(d1); ASSERT( f1 == f2 ); 

the variable f1 initialized with something that is not NaN. Is this statement guaranteed by the C ++ standard?

+6
source share
1 answer

Here are some tips, but not the answer:

4.6 The praleue value of type float can be converted to prvalue of type double. The value does not change. This conversion is called floating point promotion ....

4.8. A floating point type value can be converted to a prvalue of another floating point type. If the original value can be accurately represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent target values, the result of the conversion is the implementation-determined selection of any of these values.

+4
source

Source: https://habr.com/ru/post/984146/


All Articles