简体   繁体   English

为什么int32.maxvalue + 1会长时间溢出?

[英]Why does int32.maxvalue + 1 overflow a long?

If you put the following code in a .NET 4.5 application: 如果将以下代码放入.NET 4.5应用程序中:

public const long MAXIMUM_RANGE_MAGNITUDE = int.MaxValue + 1;

A compiler error is generated stating "The operation overflows at compile time in checked mode". 生成编译器错误,指出“操作在检查模式下在编译时溢出”。 I know that I could put this in an "unchecked" block and be fine, but my question is why does the error appear in the first place? 我知道我可以将其放在“未经检查的”块中并且可以,但是我的问题是为什么错误首先出现? Clearly a long can hold a int's max value plus one. 显然,long可以容纳int的最大值加1。

Note that using Int32 and Int64 instead of long and int does not seem to help. 请注意,使用Int32和Int64代替long和int似乎无济于事。

It is because the calculations on the right hand side of assignment is being done in integer type. 这是因为赋值右侧的计算是以整数类型进行的。 And it is overflowing integer 而且它溢出整数

You can fix that with: 您可以使用以下方法解决此问题:

public const long MAXIMUM_RANGE_MAGNITUDE = int.MaxValue + (long)1; // or 1L

By casting at least one of the operand to long 通过将至少一个操作数转换为long

The reason you get the error is specified in C# specifications. 错误发生的原因在C#规范中指定。

See C# Specification Section 4.1.5 (Integral types) 请参阅C#规范第4.1.5节(整数类型)

For the binary +, –, *, /, %, &, ^, |, ==, !=, >, <, >=, and <= operators, the operands are converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of both operands. 对于二进制+,–,*,/,%,&,^,|,==,!=,>,<,> =和<=运算符,操作数将转换为T类型,其中T为第一个int,uint,long和ulong的值,它们可以完全表示两个操作数的所有可能值。 The operation is then performed using the precision of type T, and the type of the result is T (or bool for the relational operators). 然后使用类型T的精度执行操作,结果的类型为T(或对于关系运算符为bool)。 It is not permitted for one operand to be of type long and the other to be of type ulong with the binary operators. 不允许一个操作数的类型为long,而另一个操作数的类型为ulong和二元运算符。

In your case since both operands of addition can be represented in int therefore the calculation is done in integer type. 在您的情况下,由于两个加法操作数都可以int表示,因此计算以整数类型完成。 Explicitly casting one of the operand to long would result in long result and thus no overflow error. 将操作数之一显式转换为long会导致long结果,因此不会产生溢出错误。

Your code in fact looks like this: 实际上,您的代码如下所示:

(long)(int.MaxValue + 1)

But because .Net framework has an inbuilt implicit conversion between int and long you do not have to explicitly put a cast to long in your code. 但是,由于.Net框架在int和long之间进行了内建的隐式转换,因此您不必在代码中明确地将强制类型转换为long。

So firstly this part of the code is executed: 因此,首先执行这部分代码:

int.MaxValue + 1

and the result of this operation is an int value which causes and overflow exception. 并且此操作的结果是一个int值,该值导致和溢出异常。 So your code does not even have a chance to start the conversion from int to long. 因此,您的代码甚至没有机会启动从int到long的转换。

I think this has to do with the value of int.MaxValue + 1 being calculated before the cast to long is made. 我想,这与价值做int.MaxValue + 1剧组之前所计算出的long而成。 Certainly a long can hold the value, but because you are doing integer addition, there is no way to store the integer value of int.MaxValue + 1 in an int until the cast is made. 当然,long可以容纳该值,但是由于要进行整数加法,因此int.MaxValue + 1之前, int.MaxValue + 1的整数值存储在int中。

尝试

public const long MAXIMUM_RANGE_MAGNITUDE = (long)int.MaxValue + 1L;

将常量值强制转换为long。

public const long MAXIMUM_RANGE_MAGNITUDE = (long) int.MaxValue + 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Int32.MaxValue * Int32.MaxValue == 1? - Why does Int32.MaxValue * Int32.MaxValue == 1? 为什么Int32.MinValue - 1返回Int32.MaxValue? - Why does Int32.MinValue - 1 return Int32.MaxValue? 为什么(int)(1.0 / x),其中x = 0会导致In32.MinValue而不是Int32.MaxValue? - Why does (int)(1.0 / x) where x = 0 results in In32.MinValue rather than Int32.MaxValue? 为什么我需要检查大于Int32.MaxValue? - Why would I need to check for greater than Int32.MaxValue? 如果DirectoryInfo.GetFiles()。长度超过Int32.MaxValue怎么办? - What if DirectoryInfo.GetFiles().Length exceeds Int32.MaxValue? ServicePointManager.DefaultConnectionLimit返回Int32.MaxValue - ServicePointManager.DefaultConnectionLimit returning Int32.MaxValue IIS ServicePoint ConnectionLimit始终设置为Int32.MaxValue - IIS ServicePoint ConnectionLimit always set to Int32.MaxValue 当客户端连接到&#39;localhost&#39;上的服务时,为什么System.Net.ServicePoint.ConnectionLimit使用&#39;7FFFFFFF&#39;(Int32.MaxValue / 2147483647)? - Why System.Net.ServicePoint.ConnectionLimit uses '7FFFFFFF' (Int32.MaxValue/2147483647) when a client connects to a service on 'localhost'? 数组大小大于 Int32.MaxValue 时的 .Length 行为与 LongLength - .Length behavior vs .LongLength when array size is larger than Int32.MaxValue 为什么Int64.MaxValue返回Long? - Why does Int64.MaxValue return a Long?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM