将 float 转换为 double 并返回 float 在 C++ 中给出相同的值吗
Does converting a float to a double and back to float give the same value in C++
假设在下面的代码中
float f1 = ...;
double d1 = static_cast<double>(f1);
float f2 = static_cast<float>(d1);
ASSERT( f1 == f2 );
变量 f1
被初始化为非 NaN 的值。那么断言是否保证符合 C++ 标准?
这里有一些线索,但不是答案:
4.6 A prvalue of type float can be converted to a prvalue of type double. The value is unchanged. This conversion is called floating point promotion.
...
4.8 A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact
representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values.
假设在下面的代码中
float f1 = ...;
double d1 = static_cast<double>(f1);
float f2 = static_cast<float>(d1);
ASSERT( f1 == f2 );
变量 f1
被初始化为非 NaN 的值。那么断言是否保证符合 C++ 标准?
这里有一些线索,但不是答案:
4.6 A prvalue of type float can be converted to a prvalue of type double. The value is unchanged. This conversion is called floating point promotion. ...
4.8 A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values.