Note that there are some explanatory texts on larger screens.

plurals
  1. POThe best cross platform (portable) arbitrary precision math library
    primarykey
    data
    text
    <p>I'm looking for a good arbitrary precision math library in C or C++. Could you please give me some advices / suggestions?</p> <p>The primary requirements:</p> <ol> <li>It <b>MUST</b> 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).</li> <li>The precision <b>MUST NOT NEED</b> to be specified during library initialization / object creation. The precision should <b>ONLY</b> be constrained by the available resources of the system.</li> <li>It <b>SHOULD</b> 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 <b>SHOULD NOT</b> calculate this in the same way as it does with 2^66 + 2^65 on the same platform.</li> <li>It <b>MUST</b> 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 <a href="http://en.wikipedia.org/wiki/Symbolic_computation" rel="noreferrer">symbolic computations</a> is even better. </ol> <p>Here are what I found so far:</p> <ol> <li><b>Java</b>'s <b><a href="http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html" rel="noreferrer">BigInteger</a></b> and <b><a href="http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html" rel="noreferrer">BigDecimal</a></b> 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.</li> <li>The built-in integer type or in core libraries of <b><a href="http://www.gnu.org/software/bc/manual/html_mono/bc.html" rel="noreferrer">bc</a></b> / <b><a href="http://docs.python.org/release/3.1.2/reference/lexical_analysis.html#integer-literals" rel="noreferrer">Python</a></b> / <b>Ruby</b> / <b>Haskell</b> / <b>Lisp</b> / <b>Erlang</b> / <b>OCaml</b> / <b>PHP</b> / 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.</li> </ol> <p>What I have already known:</p> <ol> <li>Using a <b><i>char</i></b> as a decimal digit, and a <b><i>char*</i></b> as a decimal string and do calculations on the digits using a for-loop.</li> <li>Using an <b><i>int</i></b> (or a <b><i>long int</i></b>, or a <b><i>long long</i></b>) as a basic "unit" and an array of it as an arbitrary long integer, and do calculations on the elements using a for-loop.</li> <li>Using an integer type to store a decimal digit (or a few digits) as <b><a href="http://en.wikipedia.org/wiki/Binary-coded_decimal" rel="noreferrer">BCD (Binary-coded decimal)</a></b>.</li> <li><b><a href="http://en.wikipedia.org/wiki/Booth%27s_multiplication_algorithm" rel="noreferrer">Booth's multiplication algorithm</a></b></li> </ol> <p>What I don't know:</p> <ol> <li> 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 <b><i>char*</i></b> string mentioned above to store the intermediate decimal results). </li> </ol> <p>What I appreciate:</p> <ol> <li>Good comparisons on <b><a href="http://gmplib.org/" rel="noreferrer">GMP</a></b>, <b><a href="http://www.mpfr.org/" rel="noreferrer">MPFR</a></b>, <b><a href="http://speleotrove.com/decimal/decnumber.html" rel="noreferrer">decNumber</a></b> (or other libraries that are good in your opinion).</li> <li>Good suggestions on books / articles that I should read. For example, an illustration with figures on how an <b>un-naive</b> binary to decimal conversion algorithm works is good. The article <b><a href="http://www.cs.uiowa.edu/~jones/bcd/decimal.html" rel="noreferrer">"Binary to Decimal Conversion in Limited Precision"</a></b> by Douglas W. Jones is an example of a good article.</li> <li>Any help.</li> </ol> <p>Please <b>DO NOT</b> answer this question if:</p> <ol> <li>you think using a <b><i>double</i></b> (or a <b><i>long double</i></b>, or a <b><i>long long double</i></b>) can solve this problem easily. If you do think so, it means that you don't understand the issue under discussion.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COAs far as I can see, GMP seems to be a good library. What I wonder is why there's a need for the contributors of Python / Haskell / Erlang / etc to re-invent the wheel. If GMP is so good, why don't rely on it? The GPL / LGPL license may be one of the issues, but despite of this (and also the rounding mode issue), are there any other disadvantages of GMP? Is the built-in integer of Python / Haskell / Erlang / any cryptography library faster than GMP? If so, I would like to extract and use it, if license permits.
      singulars
    2. COI found a nice article at http://www.cs.uiowa.edu/~jones/bcd/decimal.html by Douglas W. Jones. The article describes a method to convert a 16-bit integer to decimal representation using only 8-bit integer arithmetic. The idea is to break the 16-bit number into 4 nibbles, each representing a base-16 "digit". So, digit-0 (n0) represents x1, n1 => x16, n2 => x256, n3 => x4096. Then, it is obvious that digit-0 of the decimal number (d0) is digit-0 of the result of n0 * 1 + n1 * 6 + n2 * 6 + n3 * 6. By handling the carry properly, d1 to d4 can also be computed.
      singulars
    3. COHowever, as far as I could imagine, Douglas's idea above could not be extended to handle arbitrarily long binary integers. It is because the numbers 1 (16^0), 16 (16^1), 256 (16^2) and 4096 (16^3) are pre-calculated. The problem then becomes how to represent 16^n in decimal for arbitrarily large n.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload