在 C# 中使用 BigInteger
我正在测试 BigIntegers。
当我取一个大奇数并将其除以 2 时,我得到一个整数作为答案,没有任何迹象表明它无法精确地除以该数字。
所以第一个问题是我怎么知道两个数字可以整除。
然后我用一个小数字测试了它,这个代码:
string myNumberAsString = "25";
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
byte[] myNumberAsByteArray = encoding.GetBytes(myNumberAsString);
BigInteger myNumber = new BigInteger(myNumberAsByteArray);
Console.WriteLine(myNumber / 2);
给出结果 6809。有人知道为什么或者可以看到我的代码有什么问题吗?
我正在使用 BigInteger 的 .net 4.0 实现
I am testing out BigIntegers.
When I take a large odd number and divide it by 2, I get an integer as an anwser, without any indication that it could not divide the number exactly.
So first question is how do I know that two numbers divide exactly.
I then tested it with a small number, an this code:
string myNumberAsString = "25";
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
byte[] myNumberAsByteArray = encoding.GetBytes(myNumberAsString);
BigInteger myNumber = new BigInteger(myNumberAsByteArray);
Console.WriteLine(myNumber / 2);
Gives the result 6809. Anybody know why or can see what is wrong with my code?
I am using the .net 4.0 implementation of BigInteger
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果必须从数字的字符串表示形式进行转换,请使用 BigInteger.TryParse 或 BigInteger.Parse。
但无论您如何实例化
BigInteger
,您都可以使用模数学来确定一个数字是否可以被另一个数字整除。例如,如果您想查看someNumber
是否能被 2 整除,那么只需验证(someNumber % 2) == 0
(即 sumNumber / 2 有余数为零)。这适用于任何整数分母。只需将2
替换为您想要测试的分母即可。不过,对于 BigInteger,您可能应该使用 DivRem 方法而不是 % 运算符。If you must convert from a string representation of a number, use
BigInteger.TryParse
orBigInteger.Parse
.But regardless of how you instantiate your
BigInteger
, you can determine if a number is evenly divisible by another by using modular math. For example, if you want to see ifsomeNumber
is divisible by 2, then just verify that(someNumber % 2) == 0
(i.e., sumNumber / 2 has a remainder of zero). This works for any integer denominator. Just replace2
with which ever denominator you want to test. WithBigInteger
, though, you should probably use theDivRem
method instead of the % operator.您构建 BigInteger 的方式过于复杂 - 该框架提供了来自
byte
、Int16
等的隐式转换:从字符串转换更大的数字表示,使用 BigInteger.Parse():
You're overcomplicating how you construct the
BigInteger
- the framework provides implicit casts frombyte
,Int16
, etc:To convert larger numbers from a string representation, use
BigInteger.Parse()
:除了其他人指出的字符串到 BigInteger 转换问题之外,除两个 BigInteger 总是会产生 BigInteger 结果(因为整数没有小数部分)。该结果将是浮点结果的整数部分。
要确定除法是否精确,请使用 DivRem ()方法:
Aside from the string-to-BigInteger conversion problems that others have pointed out, dividing two BigIntegers always yields a BigInteger result (since integers don't have a fractional component). This result will be the integer part of whatever the floating-point result would have been.
To determine whether the division was exact or not, use the DivRem() method:
我不知道您使用的是什么
BigInteger
实现,但myNumberAsByteArray
不会包含表示数字25
的字节。您只需在这里将字符串转换为字节即可。您可以使用字符串myNumberAsString = "abc";
这会给您另一个结果。您可能想改用 Parse 方法:
I don't know what implementation of
BigInteger
you are using butmyNumberAsByteArray
will not contain the bytes representing the number25
. You are simply converting a string to bytes here. You could have used stringmyNumberAsString = "abc";
which would have given you another result.You probably want to use the Parse method instead: