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

时间:2021-11-07 16:52:02

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 MUST handle 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. 它必须处理任意大的整数(我最关心的是整数)。如果你不知道“任意大”这个词是什么意思,想象一下“100000”这样的词吧!(100000)的阶乘。
  3. The precision MUST NOT NEED to be specified during library initialization / object creation. The precision should ONLY be constrained by the available resources of the system.
  4. 在库初始化/对象创建期间,不需要指定精度。精度应该只受系统可用资源的限制。
  5. It SHOULD utilize 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 NOT calculate this in the same way as it does with 2^66 + 2^65 on the same platform.
  6. 它应该充分利用平台的力量,并且应该自然地处理“小”数字。这意味着在64位平台上,计算2 ^ 33 + 2 ^ 32应该使用可用的64位CPU指令。图书馆不应该以同样的方式计算是2 ^ 66 + 2 ^ 65在相同的平台上。
  7. It MUST handle 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 computations is even better.
  8. 它必须有效地处理加法(+)、减法(-)、乘法(*)、整数除法(/)、余数(%)、幂(**)、递增(++)、递减(-)、gcd()、阶乘()和其他常见的整数运算。能够处理像sqrt()(平方根)、log()(对数)这样不产生整数结果的函数是一个加号。处理符号计算的能力甚至更好。

Here are what I found so far:

以下是我迄今为止的发现:

  1. Java's BigInteger and BigDecimal class: 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. Java的BigInteger和BigDecimal类:到目前为止我一直在使用它们。我读过源代码,但我不理解其中的数学原理。它可能是基于我从未学过的理论和算法。
  3. 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.
  4. 核心库的内置整数类型或bc / Python / Ruby / Haskell / Lisp / Erlang / OCaml / PHP /其他语言:我曾经使用过这些,但我不知道他们正在使用的库,或者他们使用什么样的实现。

What I have already known:

我已经知道的:

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

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).
  2. 在十进制中打印上面提到的二进制数组,而不使用简单的方法。一个简单方法的例子:(1)将最小值加到最大值:1、2、4、8、16、32,…(2)使用上面提到的char* string存储中间小数结果)。

What I appreciate:

我很欣赏:

  1. Good comparisons on GMP, MPFR, decNumber (or other libraries that are good in your opinion).
  2. 在GMP、MPFR、decNumber(或其他您认为好的库)上进行良好的比较。
  3. Good suggestions on books / articles that I should read. For example, an illustration with figures on how an un-naive binary 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.
  4. 关于我应该读的书/文章的好建议。例如,用图说明非朴素的二进制到十进制转换算法如何工作是很好的。道格拉斯·w·琼斯(Douglas W. Jones)的文章《有限精度的二进制到十进制转换》(Binary to Decimal Conversion in Limited Precision)是一篇好文章的例子。
  5. Any help.
  6. 任何帮助。

Please DO NOT answer 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.
  2. 你认为使用一个double(或long double,或long long long double)可以很容易地解决这个问题。如果你这么认为,那就意味着你不理解正在讨论的问题。

5 个解决方案

#1


24  

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.

你要的是相关的书籍或文章。bignums的复杂部分是长除法。我推荐佩尔·布林克·汉森(Brinch Hansen)的论文《复读:雷区之旅》(multi - length Division Revisited)。

#2


13  

Overall, he fastest general purpose arbitrary precision library is GMP. If you want to work with floating point values, look at the the MPFR library. 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 GMPY module lets Python access the GMP library.

对于其他语言的本地任意精度支持,Python使用它自己的实现,因为许可证、代码大小和代码可移植性的原因。GMPY模块允许Python访问GMP库。

casevh

casevh

#3


7  

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 shootout is based on GMP.

我并没有将任意精确的算术库与我自己进行比较,但是有些人似乎或多或少都一致地使用GMP。值得注意的是,GHC Haskell和GNU Guile方案中的任意精度整数都是使用GMP实现的,并且在语言点球大战中,pi位数基准的最快实现是基于GMP的。

#4


7  

If you question actuality yet, see also http://ttmath.org

如果您还对现状提出疑问,请参见http://ttmath.org

#5


3  

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/

#1


24  

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.

你要的是相关的书籍或文章。bignums的复杂部分是长除法。我推荐佩尔·布林克·汉森(Brinch Hansen)的论文《复读:雷区之旅》(multi - length Division Revisited)。

#2


13  

Overall, he fastest general purpose arbitrary precision library is GMP. If you want to work with floating point values, look at the the MPFR library. 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 GMPY module lets Python access the GMP library.

对于其他语言的本地任意精度支持,Python使用它自己的实现,因为许可证、代码大小和代码可移植性的原因。GMPY模块允许Python访问GMP库。

casevh

casevh

#3


7  

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 shootout is based on GMP.

我并没有将任意精确的算术库与我自己进行比较,但是有些人似乎或多或少都一致地使用GMP。值得注意的是,GHC Haskell和GNU Guile方案中的任意精度整数都是使用GMP实现的,并且在语言点球大战中,pi位数基准的最快实现是基于GMP的。

#4


7  

If you question actuality yet, see also http://ttmath.org

如果您还对现状提出疑问,请参见http://ttmath.org

#5


3  

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/