C++ 最好的跨平台(便携式)任意精度数学库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2568446/
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-27 23:56:38  来源:igfitidea点击:

The best cross platform (portable) arbitrary precision math library

c++cbigintegerbigdecimalgmp

提问by Siu Ching Pong -Asuka Kenji-

I'm looking for a good arbitrary precision math library in C or C++. Could you please give me some advices / suggestions?

我正在寻找一个很好的 C 或 C++ 中的任意精度数学库。你能给我一些意见/建议吗?

The primary requirements:

主要要求:

  1. It MUSThandle arbitrarily big integers (my primary interest is on integers). In case that you don't know what the word arbitrarily big means, imagine something like 100000! (the factorial of 100000).
  2. The precision MUST NOT NEEDto be specified during library initialization / object creation. The precision should ONLYbe constrained by the available resources of the system.
  3. It SHOULDutilize the full power of the platform, and should handle "small" numbers natively. That means on a 64-bit platform, calculating 2^33 + 2^32 should use the available 64-bit CPU instructions. The library SHOULD NOTcalculate this in the same way as it does with 2^66 + 2^65 on the same platform.
  4. It MUSThandle addition (+), subtraction (-), multiplication (*), integer division (/), remainder (%), power (**), increment (++), decrement (--), gcd(), factorial(), and other common integer arithmetic calculations efficiently. Ability to handle functions like sqrt() (square root), log() (logarithm) that do not produce integer results is a plus. Ability to handle symbolic computationsis even better.
  1. 必须处理任意大的整数(我的主要兴趣是整数)。如果你不知道任意大这个词是什么意思,想象一下像 100000 这样的东西!(100000 的阶乘)。
  2. 精密MUST不需要库初始化/对象创建过程中指定。精度应受系统可用资源的限制。
  3. 应该利用平台的全部功能,并且应该本地处理“小”数字。这意味着在 64 位平台上,计算 2^33 + 2^32 应该使用可用的 64 位 CPU 指令。库不应以与在同一平台上使用 2^66 + 2^65 相同的方式计算此值。
  4. 必须处理加法 (+)、减法 (-)、乘法 (*)、整数除法 (/)、余数 (%)、幂 (**)、增量 (++)、减量 (--)、gcd() 、 factorial() 和其他常见的整数算术计算。能够处理不产生整数结果的函数,如 sqrt()(平方根)、log()(对数)是一个优点。处理符号计算的能力甚至更好。

Here are what I found so far:

以下是我目前发现的:

  1. Java's BigIntegerand BigDecimalclass: I have been using these so far. I have read the source code, but I don't understand the math underneath. It may be based on theories / algorithms that I have never learnt.
  2. The built-in integer type or in core libraries of bc/ Python/ Ruby/ Haskell/ Lisp/ Erlang/ OCaml/ PHP/ some other languages: I have ever used some of these, but I have no idea on which library they are using, or which kind of implementation they are using.
  1. JavaBigIntegerBigDecimal类:到目前为止我一直在使用它们。我已经阅读了源代码,但我不明白下面的数学。它可能基于我从未学过的理论/算法。
  2. bc/ Python/ Ruby/ Haskell/ Lisp/ Erlang/ OCaml/ PHP/一些其他语言的内置整数类型或核心库:我曾经使用过其中的一些,但我不知道他们使用的是哪个库,或者他们正在使用哪种实现。

What I have already known:

我已经知道的:

  1. Using a charas a decimal digit, and a char*as a decimal string and do calculations on the digits using a for-loop.
  2. Using an int(or a long int, or a long long) as a basic "unit" and an array of it as an arbitrary long integer, and do calculations on the elements using a for-loop.
  3. Using an integer type to store a decimal digit (or a few digits) as BCD (Binary-coded decimal).
  4. Booth's multiplication algorithm
  1. 使用char作为十进制数字,使用char*作为十进制字符串,并使用 for 循环对数字进行计算。
  2. 使用int(或long intlong long)作为基本“单位”,并将其数组作为任意长整数,并使用 for 循环对元素进行计算。
  3. 使用整数类型将十进制数字(或几个数字)存储为BCD(二进制编码的十进制)
  4. 布斯乘法算法

What I don't know:

我不知道的:

  1. Printing the binary array mentioned above in decimal without using naive methods. Example of a naive method: (1) add the bits from the lowest to the highest: 1, 2, 4, 8, 16, 32, ... (2) use a char*string mentioned above to store the intermediate decimal results).
  1. 在不使用简单方法的情况下以十进制打印上述二进制数组。一个naive方法的例子:(1)从低到高的位相加:1, 2, 4, 8, 16, 32, ... (2) 使用上面提到的char*字符串来存储中间的十进制结果)。

What I appreciate:

我欣赏的是:

  1. Good comparisons on GMP, MPFR, decNumber(or other libraries that are good in your opinion).
  2. Good suggestions on books / articles that I should read. For example, an illustration with figures on how an un-naivebinary to decimal conversion algorithm works is good. The article "Binary to Decimal Conversion in Limited Precision"by Douglas W. Jones is an example of a good article.
  3. Any help.
  1. GMPMPFRdecNumber(或您认为不错的其他库)进行了很好的比较。
  2. 关于我应该阅读的书籍/文章的好建议。例如,一个关于非天真的二进制到十进制转换算法如何工作的数字插图很好。Douglas W. Jones的文章“Binary to Decimal Conversion in Limited Precision”就是一篇好文章的例子。
  3. 任何帮助。

Please DO NOTanswer this question if:

如果出现以下情况,请不要回答这个问题:

  1. you think using a double(or a long double, or a long long double) can solve this problem easily. If you do think so, it means that you don't understand the issue under discussion.
  1. 您认为使用double(或long doublelong long double)可以轻松解决此问题。如果您确实如此认为,则表示您不了解所讨论的问题。

采纳答案by Norman Ramsey

GMP is the popular choice. Squeak Smalltalk has a very nice library, but it's written in Smalltalk.

GMP是流行的选择。Squeak Smalltalk 有一个非常好的库,但它是用 Smalltalk 编写的。

You asked for relevant books or articles. The tricky part of bignums is long division. I recommend Per Brinch Hansen's paper Multiple-Length Division Revisited: A Tour of the Minefield.

您要求提供相关书籍或文章。bignum 的棘手部分是长除法。我推荐 Per Brinch Hansen 的论文Multiple-Length Division Revisited: A Tour of the Minefield

回答by casevh

Overall, he fastest general purpose arbitrary precision library is GMP. If you want to work with floating point values, look at the the MPFRlibrary. MPFR is based on GMP.

总的来说,最快的通用任意精度库是GMP。如果要使用浮点值,请查看MPFR库。MPFR 基于 GMP。

Regarding native arbitrary precision support in other languages, Python uses its own implementation because of license, code size, and code portability reasons. The GMPYmodule lets Python access the GMP library.

关于其他语言的原生任意精度支持,由于许可、代码大小和代码可移植性原因,Python 使用自己的实现。该GMPY模块让Python的访问GMP库。

casevh

凯文

回答by Andrey Syrokomskiy

See http://ttmath.org

http://ttmath.org

Small templated header-only library for free personal and commercial use.

小型模板化头文件库,可免费用于个人和商业用途。

回答by Richard Barrell

I've not compared arbitrary precision arithmetic libraries to each other myself, but people who do seem to have more or less uniformly settled on GMP. For what it's worth, the arbitrary precision integers in GHC Haskell and GNU Guile Scheme are both implemented using GMP, and the fastest implementation of the pidigits benchmark on the language shootoutis based on GMP.

我自己并没有将任意精度的算术库相互比较,但似乎或多或少都一致地确定了 GMP。值得一提的是,GHC Haskell 和 GNU Guile Scheme 中的任意精度整数都是使用 GMP 实现的,而语言枪战中 pidigits 基准测试的最快实现是基于 GMP。

回答by fortran

What about Pari? It's built on top GMP and provides all the other goodies about number theory operations you'll ever need (and many symbolic computation stuff).

帕里呢?它建立在顶级 GMP 之上,并提供了您永远需要的有关数论运算的所有其他优点(以及许多符号计算内容)。

http://pari.math.u-bordeaux.fr/

http://pari.math.u-bordeaux.fr/