你如何处理大于 UInt64 (C#) 的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1298787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:51:03  来源:igfitidea点击:

How do you deal with numbers larger than UInt64 (C#)

c#uint64address-space

提问by Alex

In C#, how can one store and calculate with numbers that significantly exceed UInt64's max value (18,446,744,073,709,551,615)?

在 C# 中,如何存储和计算显着超过 UInt64 的最大值 (18,446,744,073,709,551,615) 的数字?

采纳答案by Steve Gilham

By using a BigInteger class; there's one in the the J# libraries (definitely accessible from C#), another in F# (need to test this one), and there are freestanding implementations such as this onein pure C#.

通过使用 BigInteger 类;J# 库中有一个(绝对可以从 C# 访问),另一个在 F# 中(需要测试这个),还有一些独立的实现,比如纯 C# 中的这个。

回答by Josip Medved

Decimal has greater range.

十进制有更大的范围。

There is support for bigInteger in .NET 4.0 but that is still not out of beta.

.NET 4.0 中支持 bigInteger,但它仍然没有超出测试版。

回答by Jon Skeet

Can you use the .NET 4.0 beta? If so, you can use BigInteger.

您可以使用 .NET 4.0 测试版吗?如果是这样,您可以使用BigInteger.

Otherwise, if you're sticking within 28 digits, you can use decimal- but be aware that obviously that's going to perform decimal arithmetic, so you may need to round at various places to compensate.

否则,如果您坚持在 28 位以内,则可以使用decimal- 但请注意,这显然将执行十进制算术,因此您可能需要在各个位置进行四舍五入以进行补偿。

回答by Filip Navara

There are several libraries for computing with big integers, most of the cryptography libraries also offer a class for that. See thisfor a free library.

有几个用于计算大整数的库,大多数密码学库也为此提供了一个类。请参阅免费图书馆。

回答by Wael Dalloul

You can use decimal. It is greater than Int64.

您可以使用十进制。它大于 Int64。

It has 28-29 significant digits.

它有 28-29 位有效数字。

回答by Pete

What is it that you wish to use these numbers for? If you are doing calculations with really big numbers, do you still need the accuracy down to the last digit? If not, you should consider using floating point values instead. They can be huge, the max value for the double type is 1.79769313486231570E+308, (in case you are not used to scientific notation it means 1.79769313486231570 multiplied by 10000000...0000 - 308 zeros).

你想用这些数字做什么?如果您正在使用非常大的数字进行计算,您还需要精确到最后一位吗?如果没有,您应该考虑改用浮点值。它们可能很大,double 类型的最大值为 1.79769313486231570E+308,(如果您不习惯科学记数法,则表示 1.79769313486231570 乘以 10000000...0000 - 308 个零)。

That should be large enough for most applications

对于大多数应用程序来说应该足够大

回答by pomber

BigIntegerrepresents an arbitrarily large signed integer.

BigInteger表示任意大的有符号整数。

using System.Numerics;

var a = BigInteger.Parse("91389681247993671255432112000000");
var b = new BigInteger(1790322312);
var c = a * b;

回答by tomRedox

Also, do check that you truly need a variable with greater capacity than Int64 and aren't falling foul of C#'s integer arithmetic.

另外,请检查您是否确实需要一个容量比 Int64 更大的变量,并且没有违反 C# 的整数算法。

For example, this code will yield an overflow error:

例如,此代码将产生溢出错误:

Int64 myAnswer = 20000*1024*1024;

At first glance that might seem to be because the result is too large for an Int64variable, but actually it's because each of the numbers on the right side of the formula are implicitly typed as Int32so the temporary memory space reserved for the result of the calculation will be Int32size, and that's what causes the overflow.

乍一看似乎是因为结果对于Int64变量来说太大了,但实际上是因为公式右侧的每个数字都是隐式键入的,Int32因此为计算结果保留的临时内存空间将是Int32大小,这就是导致溢出的原因。

The result will actually easily fit into an Int64, it just needs one of the values on the right to be cast to Int64:

结果实际上很容易适合Int64,它只需要将右侧的值之一强制转换为Int64

Int64 myAnswer = (Int64)20000*1024*1024;

This is discussed in more detail in this answer.

在这个答案更详细地讨论了这一点

(I appreciate this doesn't apply in the OP's case, but it was just this sort of issue that brought me here!)

(我很欣赏这不适用于 OP 的情况,但正是这种问题将我带到了这里!)