views:

458

answers:

6

Various questions (like this, and this) ask about how to handle integer values exceeding the native types of a particular language.

Please reply here, with one response per language, recommendations for libraries designed for handling very large numbers in various languages.

Where languages have built-in support for large numbers that's also noteworthy.

EDIT: for each language (or library) please show a code example of

  1. how the library is imported / loaded
  2. how such integer values declared
  3. a sample operation on a pair of such values (i.e. builtin operator vs method call, etc)
+3  A: 

Scheme, Lisp, and most other functional languages are great for extremely large number calculations.

For other languages, you can check out this Wikipedia page, and find libraries that would allow arbitrary precision arthmetic: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

mmattax
+3  A: 

Many languages have bindings for external libraries. One of the well-known libraries dealing with large integers is GMP.

Ruby has builtin support for large integers through its FixInt and BigInt classes.

Keltia
A: 

http://www.go-mono.com/docs/index.aspx?tlink=0@N%3aMono.Math

Mono has a BigInteger class that work with .net

AndreasN
A: 

Any open source encryption package supporting public key encryption will also have support for large integer arithmetic, and usually very well optimised (often with some assembly level support) as speed is an issue. For example look at openssl.

edit: these packages will also have good support for things like modular arithmetic, prime testing and generation, and modular exponentiation (all hard to do fast), as they feature in a lot of crypto schemes.

I believe Java also has class library support for this.

frankodwyer
+1  A: 

Another one for .NET is F# BigNum (of course it can be used from any .NET language)

Mauricio Scheffer
+1  A: 

Perl

You would use bignum, bigint, and bigrat. The best part is you don't do anything, but put 'use bignum;' at the beginning of your file.

use bignum;

$x = 2 + 4.5,"\n";                    # BigFloat 6.5
print 2 ** 512 * 0.1,"\n";            # really is what you think it is
print inf * inf,"\n";                 # prints inf
print NaN * 3,"\n";                   # prints NaN

{
  no bignum;
  print 2 ** 256,"\n";                # a normal Perl scalar now
}

# for older Perls, note that this will be global:
use bignum qw/hex oct/;
print hex("0x1234567890123490"),"\n";
print oct("01234567890123490"),"\n";
Brad Gilbert