views:

1703

answers:

10

What programming languages support arbitrary precision arithmetic and could you give a short example of how to print an arbitrary number of digits?

+1  A: 

Java natively can do bignum operations with BigDecimal. GMP is the defacto standard library for bignum with C/C++.

Andrew
Do you mean BigDecimal? BigInteger is integer only, not arbitrary-precision.
John Millikin
+2  A: 

Python has such ability. There is an excellent example here: http://tinyurl.com/python-precision

From the article:

from math import log as _flog

def log(x):
    if x < 0:
        return Decimal("NaN")
    if x == 0:
        return Decimal("-inf")
    getcontext().prec += 3
    eps = Decimal("10")**(-getcontext().prec+2)
    # A good initial estimate is needed
    r = Decimal(repr(_flog(float(x))))
    while 1:
        r2 = r - 1 + x/exp(r)
        if abs(r2-r) < eps:
            break
        else:
            r = r2
    getcontext().prec -= 3
    return +r

Also, the python quick start tutorial discusses the arbitrary precision: http://docs.python.org/lib/decimal-tutorial.html

and describes getcontext:

the getcontext() function accesses the current context and allows the settings to be changed.

Edit: Made tinyurl link to get past the dead link problem. Added clarification on getcontext.

torial
That link is dead and the code you posted doesn't work. What's getcontext()?
Matt Gregory
Added clarification inside of my answer.
torial
Thanks! I appreciate that.
Matt Gregory
+2  A: 

In Common Lisp,

(format t "~D~%" (expt 7 77))

"~D~%" in printf format would be "%d\n". Arbitrary precision arithmetic is built into Common Lisp.

sanxiyn
Is there any way to print n digits after the decimal of, say, 1/3?
Matt Gregory
If you were to print the value of (/ 1 3), it would be "1/3" or something like that. Up to the limits of available memory, you can work with precise integers and rationals. Decimal points belong with floating-point numbers, and typically Lisp systems will use standard floating point.
David Thornley
+6  A: 

Some languages have this support built in. For example, take a look at java.math.BigDecimal in Java, or decimal.Decimal in Python.

Other languages frequently have a library available to provide this feature. For example, in C you could use GMP or other options.

The "Arbitrary-precision software" section of this article gives a good rundown of your options.

Logan
Java's support for arbitrary precision arithmetic is pretty poor precisely because it is *not* part of the language. OK, BigDecimal is part of the standard library but it's not part of the language. You cannot use normal arithmetic operators with BigDecimals.
Dan Dyer
+2  A: 

Many people recommended Python's decimal module, but I would recommend using mpmath over decimal for any serious numeric uses.

sanxiyn
A: 

Scheme (a variation of lisp) has a capability called 'bignum'. there are many good scheme implementations available both full language environments and embeddable scripting options. a few I can vouch for

MitScheme (also referred to as gnu scheme) PLTScheme Chezscheme Guile (also a gnu project) Scheme 48

jottos
+1  A: 

If you want to work in the .NET world you can use still use the java.math.BigDecimal class. Just add a reference to vjslib (in the framework) and then you can use the java classes.

The great thing is, they can be used fron any .NET language. For example in C#:

    using java.math;

    namespace MyNamespace
    {
        class Program
        {
            static void Main(string[] args)
            {
                BigDecimal bd = new BigDecimal("12345678901234567890.1234567890123456789");

                Console.WriteLine(bd.ToString());
            }
        }
    }
Peter Hession
I don't see vjslib anywhere in C# Express. Where do you get it?
Matt Gregory
+1  A: 

COBOL

77 VALUE PIC S9(4)V9(4).

a signed variable witch 4 decimals.

PL/1

DCL VALUE DEC FIXED (4,4);

:-) I can't remember the other old stuff...

Jokes apart, as my example show, I think you shouldn't choose a programming language depending on a single feature. Virtually all decent and recent language support fixed precision in some dedicated classes.

Oli
You should if all you want is use the language for is that one task.
Matt Gregory
+3  A: 

Mathematica.

N[Pi, 100]

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

Not only does mathematica have arbitrary precision but by default it has infinite precision. It keeps things like 1/3 as rationals and even expressions involving things like Sqrt[2] it maintains symbolically until you ask for a numeric approximation, which you can have to any number of decimal places.

dreeves
A: 

Apparently Tcl also has them, from version 8.5, courtesy of LibTomMath:

http://wiki.tcl.tk/5193
http://www.tcl.tk/cgi-bin/tct/tip/237.html
http://math.libtomcrypt.com/
cheduardo