算术运算导致溢出。 (整数相加)

发布于 2024-10-13 01:03:45 字数 428 浏览 7 评论 0原文

我无法理解此错误:

在对方法 SetVolume 的调用中,Volume = 2055786000 和 size = 93552000。 Volume 是 Integer 属性,size 也是 Integer,如下所示你可以看到。

该类是 dbml 实体类的分部类,但是此 Volume 属性不是数据库中的列,它仅作为“业务对象属性”存在于分部类中。

查看详细信息显示:

数据> Item:为了评估索引属性,该属性必须经过限定,并且参数必须由用户显式提供。

alt text

什么可能导致此...?

I can't understand this error:

In this call to method SetVolume, Volume = 2055786000 and size = 93552000. Volume is an Integer property, and size is also Integer, as you can see.

The class is a partial class of a dbml entity class, however this Volume property is NOT a column in the database, it exist only in the partial class, as a "business object property".

View Detail shows:

Data > Item : In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.

alt text

What may cause this...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

鹤舞 2024-10-20 01:03:45

整数(有符号)的最大值为 2147483647。如果该值溢出,则会引发异常以防止程序出现意外行为。

如果不会引发该异常,您的 Volume 的值为 -2145629296,这很可能是不需要的。

解决方案:为您的卷使用 Int64。如果最大值为 9223372036854775807,您可能会更加安全。

The maximum value of an integer (which is signed) is 2147483647. If that value overflows, an exception is thrown to prevent unexpected behavior of your program.

If that exception wouldn't be thrown, you'd have a value of -2145629296 for your Volume, which is most probably not wanted.

Solution: Use an Int64 for your volume. With a max value of 9223372036854775807, you're probably more on the safe side.

浅语花开 2024-10-20 01:03:45
int.MaxValue = 2147483647
2055786000 + 93552000 = 2149338000 > int.MaxValue

所以你不能将这个数字存储为整数。您可以使用 Int64 类型,其最大值为 9,223,372,036,854,775,807

int.MaxValue = 2147483647
2055786000 + 93552000 = 2149338000 > int.MaxValue

So you cannot store this number into an integer. You could use Int64 type which has a maximum value of 9,223,372,036,854,775,807.

咆哮 2024-10-20 01:03:45

为简单起见,我将使用字节:

byte a=250;
byte b=8;
byte c=a+b;

如果 a、b 和 c 为“int”,则预期结果为 258,但在“byte”的情况下,预期结果将为 2(258 和 0xFF),但在 Windows 中在应用程序中,您会遇到异常,在控制台中则可能不会(我没有,但这可能取决于 IDE,我使用 SharpDevelop)。

然而,有时这种行为是需要的(例如,您只关心结果的低 8 位)。

您可以执行以下操作:

byte a=250;
byte b=8;

byte c=(byte)((int)a + (int)b);

这样,“a”和“b”都会转换为“int”,添加,然后转换回“byte”。

为了安全起见,您可能还想尝试:

...
byte c=(byte)(((int)a + (int)b) & 0xFF);

或者如果您确实想要这种行为,执行上述操作的更简单的方法是:

unchecked
{
    byte a=250;
    byte b=8;
    byte c=a+b;
}

或者首先声明变量,然后在“未检查”部分中进行数学运算。

或者,如果您想强制检查溢出,请改用“checked”。

希望这能解决问题。

努尔奇

相信我,那个例外是你的朋友:)

For simplicity I will use bytes:

byte a=250;
byte b=8;
byte c=a+b;

if a, b, and c were 'int', you would expect 258, but in the case of 'byte', the expected result would be 2 (258 & 0xFF), but in a Windows application you get an exception, in a console one you may not (I don't, but this may depend on IDE, I use SharpDevelop).

Sometimes, however, that behaviour is desired (e.g. you only care about the lower 8 bits of the result).

You could do the following:

byte a=250;
byte b=8;

byte c=(byte)((int)a + (int)b);

This way both 'a' and 'b' are converted to 'int', added, then casted back to 'byte'.

To be on the safe side, you may also want to try:

...
byte c=(byte)(((int)a + (int)b) & 0xFF);

Or if you really want that behaviour, the much simpler way of doing the above is:

unchecked
{
    byte a=250;
    byte b=8;
    byte c=a+b;
}

Or declare your variables first, then do the math in the 'unchecked' section.

Alternately, if you want to force the checking of overflow, use 'checked' instead.

Hope this clears things up.

Nurchi

P.S.

Trust me, that exception is your friend :)

路还长,别太狂 2024-10-20 01:03:45

结果整数值超出了整数数据类型可以容纳的范围。

尝试使用 Int64

The result integer value is out of the range which an integer data type can hold.

Try using Int64

夏见 2024-10-20 01:03:45

fo int 的最大值为 2147483647,因此 2055786000+93552000 > 2147483647 并导致溢出

Maximum value fo int is 2147483647, so 2055786000+93552000 > 2147483647 and it caused overflow

人海汹涌 2024-10-20 01:03:45

当由于除以零而将值返回为 -1.#IND 时,我发生了此错误。
有关 C++ 中 IEEE 浮点异常的更多信息 此处SO约翰·库克

对于那些否决了这个答案的人(并且做了没有具体说明原因),这个答案对某些人来说很重要的原因是除以零将导致无限大的数字,因此该值不适合 Int32 (甚至 Int64)。
因此,您收到的错误将是相同的(算术运算导致溢出),但原因略有不同。

This error occurred for me when a value was returned as -1.#IND due to a division by zero.
More info on IEEE floating-point exceptions in C++ here on SO and by John Cook

For the one who has downvoted this answer (and did not specify why), the reason why this answer can be significant to some is that a division by zero will lead to an infinitely large number and thus a value that doesn't fit in an Int32 (or even Int64).
So the error you receive will be the same (Arithmetic operation resulted in an overflow) but the reason is slightly different.

杀手六號 2024-10-20 01:03:45

2055786000 + 93552000 = 2149338000,大于 2^31。因此,如果您使用以 4 字节编码的有符号整数,则运算结果不合适,并且会出现溢出异常。

2055786000 + 93552000 = 2149338000, which is greater than 2^31. So if you're using signed integers coded on 4 bytes, the result of the operation doesn't fit and you get an overflow exception.

忘羡 2024-10-20 01:03:45

int 的最大大小为 2147483647。您可以使用更大的 Int64/Long。

The maximum size for an int is 2147483647. You could use an Int64/Long which is far larger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文