The best cross platform (portable) arbitrary precision math library

Posted by Siu Ching Pong - Asuka Kenji on Stack Overflow See other posts from Stack Overflow or by Siu Ching Pong - Asuka Kenji
Published on 2010-04-02T18:36:40Z Indexed on 2010/04/02 18:43 UTC
Read the original article Hit count: 401

Filed under:
|
|
|
|

Dear ninjas / hackers / wizards,

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. 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 a un-naive arbitrarily long binary to decimal conversion algorithm works is good.
  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.
  2. you have no experience on arbitrary precision mathematics.

Thank you in advance!

Asuka Kenji

© Stack Overflow or respective owner

Related posts about biginteger

Related posts about bigdecimal