title: What are the best (portable) cross-platform arbitrary-precision math libraries?
body: <p>I’m looking for a good arbitrary precision math library in C or C++. Could you please give me some advices or suggestions?</p>
  <p>The primary requirements:</p>
  <ol>
  <li><p>It <strong>must</strong> 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).</p>
  </li>
  <li><p>The precision <strong>must not need</strong> to be specified during library initialization or object creation. The precision should <strong>only</strong> be constrained by the available resources of the system.</p>
  </li>
  <li><p>It <strong>should</strong> 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 <strong>should not</strong> calculate this in the same way as it does with (2^66 + 2^65) on the same platform.</p>
  </li>
  <li><p>It <strong>must</strong> efficiently handle addition (<code>+</code>), subtraction (<code>-</code>), multiplication (<code>*</code>), integer division (<code>/</code>), remainder (<code>%</code>), power (<code>**</code>), increment (<code>++</code>), decrement (<code>--</code>), GCD, factorial, and other common integer arithmetic calculations. The ability to handle functions like square root and logarithm that do not produce integer results is a plus. The ability to handle <a href="http://en.wikipedia.org/wiki/Symbolic_computation" rel="noreferrer">symbolic computations</a> is even better.</p>
  </li>
  </ol>
  <p>Here are what I found so far:</p>
  <ol>
  <li><p><strong>Java</strong>'s <strong><a href="http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html" rel="noreferrer">BigInteger</a></strong> and <strong><a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="noreferrer">BigDecimal</a></strong> 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 and algorithms that I have never learnt.</p>
  </li>
  <li><p>The built-in integer type or in core libraries of <strong><a href="http://www.gnu.org/software/bc/manual/html_mono/bc.html" rel="noreferrer">bc</a></strong>, <strong><a href="http://docs.python.org/release/3.1.2/reference/lexical_analysis.html#integer-literals" rel="noreferrer">Python</a></strong>, <strong>Ruby</strong>, <strong>Haskell</strong>, <strong>Lisp</strong>, <strong>Erlang</strong>, <strong>OCaml</strong>, <strong>PHP</strong>, some other languages: I have used some of these, but I have no idea which library they are using, or which kind of implementation they are using.</p>
  </li>
  </ol>
  <p>What I have already known:</p>
  <ol>
  <li><p>Using <code>char</code> for decimal digits and <code>char*</code> for decimal strings, and do calculations on the digits using a <code>for</code>-loop.</p>
  </li>
  <li><p>Using <code>int</code> (or <code>long int</code>, or <code>long long</code>) as a basic “unit” and an array of that type as an arbitrary long integer, and do calculations on the elements using a <code>for</code>-loop.</p>
  </li>
  <li><p>Using an integer type to store a decimal digit (or a few digits) as <strong><a href="http://en.wikipedia.org/wiki/Binary-coded_decimal" rel="noreferrer">BCD (Binary-coded decimal)</a></strong>.</p>
  </li>
  <li><p><strong><a href="http://en.wikipedia.org/wiki/Booth%27s_multiplication_algorithm" rel="noreferrer">Booth’s multiplication algorithm</a></strong>.</p>
  </li>
  </ol>
  <p>What I don’t know:</p>
  <ol>
  <li>Printing the binary array mentioned above in decimal without using naive methods. An example of a naive method: (1) add the bits from the lowest to the highest: 1, 2, 4, 8, 16, 32, … (2) use a <code>char*</code>-string mentioned above to store the intermediate decimal results).</li>
  </ol>
  <p>What I appreciate:</p>
  <ol>
  <li><p>Good comparisons on <strong><a href="http://gmplib.org/" rel="noreferrer">GMP</a></strong>, <strong><a href="http://www.mpfr.org/" rel="noreferrer">MPFR</a></strong>, <strong><a href="http://speleotrove.com/decimal/decnumber.html" rel="noreferrer">decNumber</a></strong> (or other libraries that are good in your opinion).</p>
  </li>
  <li><p>Good suggestions on books and articles that I should read. For example, an illustration with figures on how a <strong>non-naive</strong> binary-to-decimal conversion algorithm works would be good. The article “<strong><a href="http://www.cs.uiowa.edu/%7Ejones/bcd/decimal.html" rel="noreferrer">Binary to Decimal Conversion in Limited Precision</a></strong>” by Douglas W. Jones is an example of a good article.</p>
  </li>
  <li><p>Any help in general.</p>
  </li>
  </ol>
  <p>Please <strong>do not</strong> answer this question if you think that using <code>double</code> (or <code>long double</code>, or <code>long long double</code>) can solve this problem easily. If you do think so, you don’t understand the issue in question.</p>