C#:如何处理大数算术?

发布于 2024-08-20 23:39:12 字数 128 浏览 3 评论 0原文

我正在编写一个应用程序,其中涉及带有大量数字的算术运算。我之前编写过一个类,通过将大数字定义为字符串,然后使用慢速算术字符串函数来简化对大数字的处理。这是最好的方法吗?如果不是,我应该如何解决这个问题? C# 有内置的东西可以应对这种情况吗?

I'm writing an app which involves arithmetic with humongous numbers, with very many digits. I've previously written a class that simplifies handling big numbers by defining them as strings and then using slow arithmetic string functions. Is this the best way to do it? If not, how should I approach this problem? Does C# have anything built-in for such situations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮色兮凉城 2024-08-27 23:39:12

如果您可以在 .NET 4 上执行此操作,System.Numeric.BigInteger 可以提供帮助。如果您使用的是旧版本的 .NET,IntX 将为您提供帮助。

另请参阅有关 C# 中大整数的这个 SO 问题

If you can do it on .NET 4, System.Numeric.BigInteger can help. If you're on an older version of .NET, IntX will help you.

See also this this SO question on big integers in C#.

爱冒险 2024-08-27 23:39:12

.NET 4 将通过 BigInteger 类型内置此功能。据称这已经调整得很好并且应该表现得很好。

对于 3.5 及更早版本,您可以从动态语言运行时源获取 BigInteger 的实现。 (参见例如 http://dlr.codeplex.com/sourcecontrol/ changeset/view/40021?projectName=dlr#694008 并深入到 Src / Runtime / Microsoft.Dynamic / Math。)我不知道这是否已经像 .NET 4 BigInteger 类型一样高度调整,但它仍然应该比字符串版本更有效,因为它在内部使用整数类型表示大数字并使用整数运算执行算术运算。

.NET 4 will have this built in via the BigInteger type. This is claimed to be pretty well tuned and should perform very well.

For 3.5 and earlier, you can grab an implementation of BigInteger from the Dynamic Language Runtime sources. (See e.g. http://dlr.codeplex.com/sourcecontrol/changeset/view/40021?projectName=dlr#694008 and drill down into Src / Runtime / Microsoft.Dynamic / Math.) I don't know if this has been tuned as highly as the .NET 4 BigInteger type, but it should still be more efficient than your string version because it internally represents the big numbers using integral types and performs arithmetic using integer operations.

余生共白头 2024-08-27 23:39:12

GNU MP bignum 库是最快的库之一。有一个 .NET 包装器
http://gnumpnet.codeplex.com/

这是另一个bignum 库 for .NET 及其源代码。

The GNU MP bignum library is one of the fastest around. there's a .NET wrapper for it
http://gnumpnet.codeplex.com/

Here's another bignum library for .NET with source.

春风十里 2024-08-27 23:39:12

我在密码学项目中多次使用过 Big Integer 类(需要非常大的数字)。效果很好。

http://www.codeproject.com/KB/cs/biginteger.aspx

Big Integer class that I've used many times for cryptography projects (where extremely large numbers are needed). Works great.

http://www.codeproject.com/KB/cs/biginteger.aspx

只涨不跌 2024-08-27 23:39:12

在 dotnet 4.0 以上版本中,
System.Numerics.BigInteger 将帮助您解决此问题。

如果使用上述语法出现程序集引用错误,请使用

http://www.dll 添加引用。 dllme.com/dll/files/system_numerics_dll.html

希望这会帮助你..!

In dotnet version above 4.0,
System.Numerics.BigInteger will help you for this problem.

if you get assembly reference error using the above syntax, then add reference using

http://www.dllme.com/dll/files/system_numerics_dll.html.

Hope this will help you..!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文