无穷大到整数的转换是否未定义?
Is casting of infinity to integer undefined?
将无穷大(用浮点数表示)转换为整数是未定义的行为吗?
标准说:
4.10 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.
但我不知道 "truncated value cannot be represented" 是否涵盖无穷大。
我想了解为什么 std::numeric_limits<int>::infinity()
和 static_cast<int>(std::numeric_limits<float>::infinity() )
有不同的结果。
#include <iostream>
#include <limits>
int main ()
{
std::cout << std::numeric_limits<int>::infinity () << std::endl;
std::cout << static_cast<int> (std::numeric_limits<float>::infinity () ) << std::endl;
return 0;
}
输出:
0
-2147483648
std::numeric_limits<int>::infinity()
is well defined的结果等于0
,但我找不到任何关于铸造无限的信息。
无穷大到整数的转换未定义。
The behavior is undefined if the truncated value cannot be represented in the destination type.
说明了一切。由于截断会删除精度而不是幅度,因此截断后的无穷大仍然是无穷大,整数不能表示无穷大。
I'm trying to understand why std::numeric_limits<int>::infinity()
and static_cast<int>(std::numeric_limits<float>::infinity() )
have different results.
标准说:18.3.2.4
static constexpr T infinity() noexcept;
47 Representation of positive infinity, if available. [216]
48 Meaningful for all specializations for which has_infinity != false. Required in specializations for which is_iec559 != false.
--- 编辑 ---
根据 18.3.2.7/1 [numeric.special]
1 All members shall be provided for all specializations. However, many values are only required to be meaningful under certain conditions (for example, epsilon() is only meaningful if is_integer is false). Any value that is not “meaningful” shall be set to 0 or false.
你说
I can't tell whether "truncated value cannot be represented" covers infinity
但这一切都归结为
What is the result of truncating infinity.
C 标准(通过 26.9 合并到 C++ 中)非常明确地回答了这个问题:
由于无穷大的截断仍然是无穷大,并且无穷大不能在 int
中表示(我希望这部分没有问题),因此行为未定义。
将无穷大(用浮点数表示)转换为整数是未定义的行为吗?
标准说:
4.10 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.
但我不知道 "truncated value cannot be represented" 是否涵盖无穷大。
我想了解为什么 std::numeric_limits<int>::infinity()
和 static_cast<int>(std::numeric_limits<float>::infinity() )
有不同的结果。
#include <iostream>
#include <limits>
int main ()
{
std::cout << std::numeric_limits<int>::infinity () << std::endl;
std::cout << static_cast<int> (std::numeric_limits<float>::infinity () ) << std::endl;
return 0;
}
输出:
0
-2147483648
std::numeric_limits<int>::infinity()
is well defined的结果等于0
,但我找不到任何关于铸造无限的信息。
无穷大到整数的转换未定义。
The behavior is undefined if the truncated value cannot be represented in the destination type.
说明了一切。由于截断会删除精度而不是幅度,因此截断后的无穷大仍然是无穷大,整数不能表示无穷大。
I'm trying to understand why
std::numeric_limits<int>::infinity()
andstatic_cast<int>(std::numeric_limits<float>::infinity() )
have different results.
标准说:18.3.2.4
static constexpr T infinity() noexcept;
47 Representation of positive infinity, if available. [216]
48 Meaningful for all specializations for which has_infinity != false. Required in specializations for which is_iec559 != false.
--- 编辑 ---
根据 18.3.2.7/1 [numeric.special]
1 All members shall be provided for all specializations. However, many values are only required to be meaningful under certain conditions (for example, epsilon() is only meaningful if is_integer is false). Any value that is not “meaningful” shall be set to 0 or false.
你说
I can't tell whether "truncated value cannot be represented" covers infinity
但这一切都归结为
What is the result of truncating infinity.
C 标准(通过 26.9 合并到 C++ 中)非常明确地回答了这个问题:
由于无穷大的截断仍然是无穷大,并且无穷大不能在 int
中表示(我希望这部分没有问题),因此行为未定义。