c# - Double.MaxValue 为整数是否为负数?

标签 c# .net casting type-conversion

为什么 Double.MaxValue转换为整数类型会产生负值,即该类型的最小值?

double maxDouble = double.MaxValue;       // 1.7976931348623157E+308
long maxDoubleLong = (long) maxDouble;    // -9223372036854775808

如果它太大或在运行时出现 OverflowException,或者如果我使用 unchecked 转换可能不会引发异常,我会理解编译器错误,但是结果变得不确定和不正确(负)。

同样奇怪的是,这个值是long.MinValue :

bool sameAsLongMin = maxDoubleLong == long.MinValue; // true

顺便说一句,如果我将它转换为 int,也会发生同样的情况:

int maxDoubleInt = (int)maxDouble;                   // -2147483648
bool sameAsIntMin = maxDoubleInt == int.MinValue;    // true

如果它尝试将其转换为decimal,我会在运行时得到一个OverflowException

decimal maxDoubleDec = (decimal)maxDouble;  // nope

更新:Michael 和 Barre 的回答似乎一针见血,如果我明确使用 checked,我会得到一个 OverflowException:

checked
{
    double maxDouble = double.MaxValue;     // 1.7976931348623157E+308
    long maxDoubleLong = (long) maxDouble;  // nope
}

最佳答案

C# Language Specification(5.0 版)在 6.2.1“显式数字转换”(添加了强调)中说明如下:

  • For a conversion from float or double to an integral type, the processing depends on the overflow checking context (§7.6.12) in which the conversion takes place:

    • In a checked context, the conversion proceeds as follows:

      • If the value of the operand is NaN or infinite, a System.OverflowException is thrown.
      • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.
      • Otherwise, a System.OverflowException is thrown.
    • In an unchecked context, the conversion always succeeds, and proceeds as follows.

      • If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.
      • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.
      • Otherwise, the result of the conversion is an unspecified value of the destination type.

以及在 7.6.12“已检查和未检查的运算符”中

For non-constant expressions (expressions that are evaluated at run-time) that are not enclosed by any checked or unchecked operators or statements, the default overflow checking context is unchecked unless external factors (such as compiler switches and execution environment configuration) call for checked evaluation.

对于从 doubledecimal 的转换:“如果源值为 NaN、无穷大或太大而无法表示为小数,则抛出 System.OverflowException” . checked vs unchecked 没有发挥作用(那些只处理积分运算)。

关于c# - Double.MaxValue 为整数是否为负数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757239/

相关文章:

c# - asp.net mvc发布请求+服务层-最好的方法

c# - 我应该在 Dispose 上解除绑定(bind)吗?

.net - 将参数传递给 Sitecore 中的 Razor View 渲染

C++自动 'type cast'转换

java - 我如何在我的 JSP 页面中格式化这个代表十进制数字的字符串?

c# - 我可以使用 GetHashCode 跟踪对象标识吗?

c# - ListView 的 EditItemTemplate 中的 AsyncFileUpload

c# - 为什么调用我的 List<Point>.Clear() 会抛出 ArgumentOutOfRangeException?

c# - 在C#中更改Label的颜色

c - 为什么将未使用的函数参数值转换为 void?