The best cross platform (portable) arbitrary precision math library [closed]

空扰寡人 提交于 2019-11-26 03:49:17

问题


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

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. 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.
  3. 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.
  4. 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.

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. 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.

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. 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

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).

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-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.
  3. Any help.

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.

回答1:


GMP is the popular choice. Squeak Smalltalk has a very nice library, but it's written in 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.




回答2:


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.

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.

casevh




回答3:


See http://ttmath.org

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




回答4:


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.




回答5:


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).

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



来源:https://stackoverflow.com/questions/2568446/the-best-cross-platform-portable-arbitrary-precision-math-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!