乘两个正的Int32返回不正确的,否定的回答? [英] Multiplying two positive Int32 returns incorrect, negative answer?

查看:148
本文介绍了乘两个正的Int32返回不正确的,否定的回答?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的难倒就这一个。我编码在C#中的Windows Phone 7.5;我正在文本从一个文本框,它解析成一个阵列,然后使用Convert.ToInt32每个阵元变换成一个Int32,然后通过一系列数学计算的运行所产生的Int32值,相乘并添加Int32值到硬编码号码(全部取决于什么在UI中选择的)

I'm really stumped on this one. I'm coding in C# for Windows Phone 7.5; I am taking text from a text box, parsing it into an array, then converting each array element into an Int32 using Convert.ToInt32, then running the resulting Int32 values through a series of mathematical calculations, multiplying and adding the Int32 values to hardcoded numbers (all dependent upon what's selected in the UI).

一切都很好,直到我承担由此产生的计算和一起乘他们:我是从乘以2得到一个负数正数!这是我做的任何计算既源自一个使用Convert.ToInt32功能的方法数字的唯一一次。当他们加,减,甚至分歧,算算出来正确。但是,当他们乘,没有骰子。数学是完全错误的;我双重检查中的LibreOffice Calc的数学,它不匹配。当逐句通过代码,一切都正确的,除非是起源于一个使用Convert.ToInt32函数方法的数字相乘。任何想法?

All is well until I take the resulting calculations and multiply them together: I'm getting a negative number from multiplying two positive numbers! This is the only time I am doing any calculations with both of the numbers that originated in the method that uses the Convert.ToInt32 function. When they're added, subtracted, or even divided, the math comes out correctly. But when they're multiplied, no dice. The math is totally wrong; I double checked the math in LibreOffice Calc and it doesn't match. When stepping through the code, everything's correct until the numbers that originated in the method that uses the Convert.ToInt32 function are multiplied together. Any ideas?

推荐答案

数字很可能大到足以溢 Int32.MaxValue

The numbers are probably large enough to overflow Int32.MaxValue.

考虑:

var r = Int32.MaxValue / 2;     // r = 1073741823
var ans = r * 3;                // ans = -1073741827



这样做的原因是,负整数与一个<$ C表示$ C> 1 在最大比特,因此,它比最大值大会有任何乘法 1 在这个位置,这被解释为负值。

The reason for this is that negative integers are represented with a 1 in the largest bit, so any multiplication which is larger than the maximum value will have a 1 in this position, which is interpreted as a negative value.

溢出与所有整型危险( INT 总之字节为sbyte 字符 ULONG USHORT ,和 UINT )。你可以使用一个浮点类型,如浮动双击,但这种带有其他问题,如四舍五入和平等的问题。你必须知道你期待什么样的数字,那么你可能会使用其它类型,如 UINT ULONG 或浮点型就像双击小数。最后,如​​果你不知道会发生什么,你需要输入编写代码后卫的(例如把它包在检查{} [感谢,@EricLippert] 的,并处理它时,它是你设计的范围之外的值。

Overflow is a danger with all integral types (int, short, byte, sbyte, char, long, ulong, ushort, and uint). You could use a floating point type, like float or double, but this comes with other problems, like rounding and equality issues. You have to know what kind of numbers you expect, then you might use other types like long, uint, ulong or a floating point type like double or decimal. Ultimately, if you don't know what could happen, you need to write guard code on the input (e.g. wrap it in a checked { } block [thanks, @EricLippert]), and handle it when it is a value outside the range you've designed for.

编辑:在.net 4.0+另一种选择是使用 System.Numerics.BigInteger ,这避免了溢出,但也可以是慢

Another option in .Net 4.0+ is to use System.Numerics.BigInteger, which avoids any overflow, but can be slow.

这篇关于乘两个正的Int32返回不正确的,否定的回答?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆