[英]How do you deal with numbers larger than UInt64 (C#)
在 C# 中,如何存储和计算显着超过 UInt64 的最大值 (18,446,744,073,709,551,615) 的数字?
Can you use the .NET 4.0 beta?您可以使用 .NET 4.0 测试版吗? If so, you can use
BigInteger
.如果是这样,您可以使用
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 using a BigInteger class;通过使用 BigInteger 类; 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 one in pure C#.
J# 库中有一个(绝对可以从 C# 访问),另一个在 F# 中(需要测试这个),还有一些独立的实现,比如纯 C# 中的这个。
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对于大多数应用程序来说,这应该足够大
BigInteger represents an arbitrarily large signed integer. BigInteger表示任意大的有符号整数。
using System.Numerics;
var a = BigInteger.Parse("91389681247993671255432112000000");
var b = new BigInteger(1790322312);
var c = a * b;
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,但它仍然没有超出测试阶段。
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 Int64
variable, but actually it's because each of the numbers on the right side of the formula are implicitly typed as Int32
so the temporary memory space reserved for the result of the calculation will be Int32
size, 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
:
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 的情况,但正是这种问题将我带到了这里!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.