C# .Net 中的 Int128?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/227731/
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
Int128 in .Net?
提问by Adam Tegen
I need to do some large integer math. Are there any classes or structs out there that represent a 128-bit integer and implement all of the usual operators?
我需要做一些大整数数学。是否有任何表示 128 位整数并实现所有常用运算符的类或结构?
BTW, I realize that decimal can be used to represent a 96-bit int.
顺便说一句,我意识到十进制可用于表示 96 位整数。
采纳答案by Larsenal
It's here in System.Numerics. "The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds."
它在System.Numerics 中。“BigInteger 类型是一种不可变类型,它表示一个任意大的整数,其值在理论上没有上限或下限。”
var i = System.Numerics.BigInteger.Parse("10000000000000000000000000000000");
回答by Jon Skeet
No, there's nothing in .NET <= 3.5. I'm hoping/expecting that BigIntegerwill make its return in .NET 4.0. (It was cut from .NET 3.5.)
不,.NET <= 3.5 中什么都没有。我希望/期待BigInteger在 .NET 4.0 中回归。(它是从 .NET 3.5 剪下来的。)
回答by Craig
I believe Mono has a BigInteger implementation that you should be able to track down the source for.
我相信 Mono 有一个 BigInteger 实现,您应该能够追踪其来源。
回答by Joe Basirico
Here's an implementation of big integer from .net matters.
这是 .net 事务中大整数的实现。
回答by sbeskur
If you don't mind making reference to the J# library (vjslib.dll included with VS by default) there is already and implementation of BigInteger in .NET
如果您不介意参考 J# 库(vjslib.dll 默认包含在 VS 中),那么在 .NET 中已经有 BigInteger 的实现
using java.math;
public static void Main(){
BigInteger biggy = new BigInteger(....)
}
回答by Charles Burns
BigInteger is now a standard part of C# and friends in .NET 4.0. See:Gunnar Peipman's ASP.NET blog. Note that the CPU can generally work with ordinary integers much more quickly and in constant time, especially when using the usual math operators (+, -, /, ...) because these operators typically map directly to single CPU instructions.
BigInteger 现在是 C# 的标准部分,也是 .NET 4.0 中的朋友。请参阅:Gunnar Peipman 的 ASP.NET 博客。请注意,CPU 通常可以在恒定时间内更快地处理普通整数,尤其是在使用常用数学运算符(+、-、/、...)时,因为这些运算符通常直接映射到单个 CPU 指令。
With BigInteger, even the most basic math operations are much slower function calls to methods whose runtime varies with the size of the number. This is because BigInteger implements arbitrary precision arithmetic, which adds considerable but necessary overhead. The benefit is that BigIntegers are not limited to 64 or even 128 bits, but by available system memory (or about 2^64 bits of precision, whichever comes first). Read here.
使用 BigInteger,即使是最基本的数学运算,对运行时间随数字大小而变化的方法的函数调用也会慢得多。这是因为 BigInteger 实现了任意精度算术,这增加了大量但必要的开销。好处是 BigIntegers 不限于 64 位甚至 128 位,而是由可用的系统内存(或大约 2^64 位的精度,以先到者为准)。在这里阅读。
回答by Alexander Logger
C# PCL library for computations with big numbers such as Int128 and Int256. https://github.com/everbytes/BigMath
C# PCL 库,用于计算 Int128 和 Int256 等大数。 https://github.com/everbytes/BigMath
回答by Antoine Diekmann
Here's an implementation of Int128 in .NET: https://int128.codeplex.com/
这是 .NET 中 Int128 的实现:https://int128.codeplex.com/
回答by Rick Sladkey
While BigInteger
is the best solution for most applications, if you have performance critical numerical computations, you can use the complete Int128
and UInt128
implementations in my Dirichlet.Numericslibrary. These types are useful if Int64
and UInt64
are too small but BigInteger
is too slow.
虽然BigInteger
是大多数应用程序的最佳解决方案,但如果您有性能关键的数值计算,您可以使用我的Dirichlet.Numerics库中的完整Int128
和UInt128
实现。如果和太小但太慢,这些类型很有用。Int64
UInt64
BigInteger
回答by Brett Allen
GUID is backed by a 128 bit integer in .NET framework; though it doesn't come with any of the typical integer type methods.
GUID 由 .NET 框架中的 128 位整数支持;尽管它没有任何典型的整数类型方法。
I've written a handler for GUID before to treat it as a 128 bit integer, but this was for a company I worked for ~8 years ago. I no longer have access to the source code.
我之前为 GUID 编写了一个处理程序,将其视为 128 位整数,但这是为我大约 8 年前工作的公司编写的。我不再有权访问源代码。
So if you need native support for a 128 bit integer, and don't want to rely on BigInteger for whatever reason, you could probably hack GUID to server your purposes.
因此,如果您需要对 128 位整数的本机支持,并且无论出于何种原因不想依赖 BigInteger,您都可以破解 GUID 来满足您的目的。