C++ 标准为缩小从 double 到 int 的转换提供了哪些保证?
What guarantees does the C++ standard give for narrowing conversion from double to int?
C++ 标准为缩小从 double 到 int 类型的转换提供了哪些保证?
它是否与 中解释的 Java 相同:
不,这与 Java 中的不一样。如果 "truncate the fractional part" 的数学结果不能用目标类型表示,则行为未定义。
从 4.9 [conv.fpint]/1 ("Floating-integral conversions"):
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
C++ 标准为缩小从 double 到 int 类型的转换提供了哪些保证?
它是否与
不,这与 Java 中的不一样。如果 "truncate the fractional part" 的数学结果不能用目标类型表示,则行为未定义。
从 4.9 [conv.fpint]/1 ("Floating-integral conversions"):
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.