从 float 到 double 和从 double 到 float 的精度损失?
Precision loss from float to double, and from double to float?
float fv = orginal_value; // original_value may be any float value
...
double dv = (double)fv;
...
fv = (float)dv;
fv 应该等于 original_value 吗?任何精度都可能丢失?
SHOULD fv be equal to original_value exactly? Any precision may be
lost?
是的,如果 dv
的值在两者之间没有变化。
来自 C99 规范中的 Conversion 6.3.1.5 Real Floating types 部分:
- When a float is promoted to double or long double, or a double is
promoted to long double, its value is unchanged.
- When a double is
demoted to float, a long double is demoted to double or float, or a
value being represented in greater precision and range than required
by its semantic type (see 6.3.1.8) is explicitly converted to its
semantic type, if the value being converted can be represented exactly
in the new type, it is unchanged. If the value being converted is in
the range of values that can be represented but cannot be represented
exactly, the result is either the nearest higher or nearest lower
representable value, chosen in an implementation-defined manner. If
the value being converted is outside the range of values that can be
represented, the behavior is undefined
对于 C++,来自 4.6 部分又名 conv.fpprom(使用的草案:n337,我相信类似线路在最终规格中可用)
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 部分又名 conv.double
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. Otherwise,
the behavior is undefined. The conversions allowed as floating point
promotions are excluded from the set of floating point conversions
因此值应该完全相等。
float fv = orginal_value; // original_value may be any float value
...
double dv = (double)fv;
...
fv = (float)dv;
fv 应该等于 original_value 吗?任何精度都可能丢失?
SHOULD fv be equal to original_value exactly? Any precision may be lost?
是的,如果 dv
的值在两者之间没有变化。
来自 C99 规范中的 Conversion 6.3.1.5 Real Floating types 部分:
- When a float is promoted to double or long double, or a double is promoted to long double, its value is unchanged.
- When a double is demoted to float, a long double is demoted to double or float, or a value being represented in greater precision and range than required by its semantic type (see 6.3.1.8) is explicitly converted to its semantic type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined
对于 C++,来自 4.6 部分又名 conv.fpprom(使用的草案:n337,我相信类似线路在最终规格中可用)
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 部分又名 conv.double
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. Otherwise, the behavior is undefined. The conversions allowed as floating point promotions are excluded from the set of floating point conversions
因此值应该完全相等。