21

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

kennytm
  • 469,458
  • 94
  • 1,022
  • 977
Matt Gregory
  • 6,184
  • 5
  • 29
  • 36

18 Answers18

14

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
  • 1,796
  • 1
  • 11
  • 11
  • 7
    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 Sep 17 '09 at 13:41
  • Exactly what @DanDyer said. The question specifically asks about _languages_ which support arbitrary-precision arithmetic, which Java does not. `BigDecimal` is someone using the language to implement _their own_ arbitrary-precision arithmetic class/library. But I'd suspect that most any nontrivial language would have such a library available. It's not what the question asks. – aroth Jun 17 '17 at 12:46
13

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
  • 25,132
  • 42
  • 147
  • 226
6

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
  • 3,570
  • 1
  • 17
  • 14
  • Is there any way to print n digits after the decimal of, say, 1/3? – Matt Gregory Sep 27 '08 at 11:20
  • 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 Sep 17 '09 at 13:46
6

Smalltalk supports arbitrary precision Integers and Fractions from the beginning. Note that gnu Smalltalk implementation does use GMP under the hood. I'm also developping ArbitraryPrecisionFloat for various dialects (Squeak/Pharo Visualworks and Dolphin), see http://www.squeaksource.com/ArbitraryPrecisionFl.html

5

Python has such ability. There is an excellent example here.

From the article:

from math import log as _flog
from decimal import getcontext, Decimal

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: Added clarification on getcontext.

larsr
  • 5,061
  • 16
  • 32
torial
  • 12,879
  • 9
  • 58
  • 88
5

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

sanxiyn
  • 3,570
  • 1
  • 17
  • 14
  • As its name indicates it, Decimal is intended to use decimal (base 10) numbers, which is very relevant for some purposes (financial computations, etc.) but not very efficient for artihmetic purposes. Other modules perform arithmetic on base-2 related blocks of digits. mpmath and gmpy are very efficient when speed is an issue. – Thomas Baruchel Dec 05 '14 at 14:43
3

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
  • 19,028
  • 9
  • 28
  • 26
3

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
  • 1,702
  • 3
  • 19
  • 22
3

Ruby whole numbers and floating point numbers (mathematically speaking: rational numbers) are by default not strictly tied to the classical CPU related limits. In Ruby the integers and floats are automatically, transparently, switched to some "bignum types", if the size exceeds the maximum of the classical sizes.

One probably wants to use some reasonably optimized and "complete", multifarious, math library that uses the "bignums". This is where the Mathematica-like software truly shines with its capabilities.

As of 2011 the Mathematica is extremely expensive and terribly restricted from hacking and reshipping point of view, specially, if one wants to ship the math software as a component of a small, low price end, web application or an open source project. If one needs to do only raw number crunching, where visualizations are not required, then there exists a very viable alternative to the Mathematica and Maple. The alternative is the REDUCE Computer Algebra System, which is Lisp based, open source and mature (for decades) and under active development (in 2011). Like Mathematica, the REDUCE uses symbolic calculation.

For the recognition of the Mathematica I say that as of 2011 it seems to me that the Mathematica is the best at interactive visualizations, but I think that from programming point of view there are more convenient alternatives even if Mathematica were an open source project. To me it seems that the Mahtematica is also a bit slow and not suitable for working with huge data sets. It seems to me that the niche of the Mathematica is theoretical math, not real-life number crunching. On the other hand the publisher of the Mathematica, the Wolfram Research, is hosting and maintaining one of the most high quality, if not THE most high quality, free to use, math reference sites on planet Earth: the http://mathworld.wolfram.com/ The online documentation system that comes bundled with the Mathematica is also truly good.

When talking about speed, then it's worth to mention that REDUCE is said to run even on a Linux router. The REDUCE itself is written in Lisp, but it comes with 2 of its very own, specific, Lisp implementations. One of the Lisps is implemented in Java and the other is implemented in C. Both of them work decently, at least from math point of view. The REDUCE has 2 modes: the traditional "math mode" and a "programmers mode" that allows full access to all of the internals by the language that the REDUCE is self written in: Lisp.

So, my opinion is that if one looks at the amount of work that it takes to write math routines, not to mention all of the symbolic calculations that are all MATURE in the REDUCE, then one can save enormous amount of time (decades, literally) by doing most of the math part in REDUCE, specially given that it has been tested and debugged by professional mathematicians over a long period of time, used for doing symbolic calculations on old-era supercomputers for real professional tasks and works wonderfully, truly fast, on modern low end computers. Neither has it crashed on me, unlike at least one commercial package that I don't want to name here.

http://www.reduce-algebra.com/

To illustrate, where the symbolic calculation is essential in practice, I bring an example of solving a system of linear equations by matrix inversion. To invert a matrix, one needs to find determinants. The rounding that takes place with the directly CPU supported floating point types, can render a matrix that theoretically has an inverse, to a matrix that does not have an inverse. This in turn introduces a situation, where most of the time the software might work just fine, but if the data is a bit "unfortunate" then the application crashes, despite the fact that algorithmically there's nothing wrong in the software, other than the rounding of floating point numbers.

The absolute precision rational numbers do have a serious limitation. The more computations is performed with them, the more memory they consume. As of 2011 I don't know any solutions to that problem other than just being careful and keeping track of the number of operations that has been performed with the numbers and then rounding the numbers to save memory, but one has to do the rounding at a very precise stage of the calculations to avoid the aforementioned problems. If possible, then the rounding should be done at the very end of the calculations as the very last operation.

3

In PHP you have BCMath. You not need to load any dll or compile any module. Supports numbers of any size and precision, represented as string

<?php

$a = '1.234';
$b = '5';

echo bcadd($a, $b);     // 6
echo bcadd($a, $b, 4);  // 6.2340

?>
McDowell
  • 102,869
  • 29
  • 193
  • 261
2

There are several Javascript libraries that handle arbitrary-precision arithmetic.
For example, using my big.js library:

Big.DP = 20;                      // Decimal Places
var pi = Big(355).div(113)    

console.log( pi.toString() );    // '3.14159292035398230088'
MikeM
  • 9,855
  • 2
  • 27
  • 42
2

In R you can use the Rmpfr package:

library(Rmpfr)
exp(mpfr(1, 120))
## 1 'mpfr' number of precision  120   bits 
## [1] 2.7182818284590452353602874713526624979

You can find the vignette here: Arbitrarily Accurate Computation with R: The Rmpfr Package

vonjd
  • 3,530
  • 3
  • 36
  • 57
2

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
  • 5,849
  • 1
  • 17
  • 11
1

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

Andrew
  • 1,216
  • 10
  • 17
1

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
  • 491
  • 5
  • 9
1

The (free) basic program x11 basic ( http://x11-basic.sourceforge.net/ ) has arbitrary precision for integers. (and some useful commands as well, e.g. nextprime( abcd...pqrs))

jmorika
  • 11
  • 1
1

IBM's interpreted scripting language Rexx, provides custom precision setting with Numeric. https://www.ibm.com/docs/en/zos/2.1.0?topic=instructions-numeric. The language runs on mainframes and pc operating systems and has very powerful parsing and variable handling as well as extension packages. Object Rexx is the most recent implementation. Links from https://en.wikipedia.org/wiki/Rexx

0

Haskell has excellent support for arbitrary-precision arithmetic built in, and using it is the default behavior. At the REPL, with no imports or setup required:

Prelude> 2 ^ 2 ^ 12
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336

(try this yourself at https://tryhaskell.org/)

If you're writing code stored in a file and you want to print a number, you have to convert it to a string first. The show function does that.

module Test where

main = do
  let x = 2 ^ 2 ^ 12
  let xStr = show x
  putStrLn xStr

(try this yourself at code.world: https://www.code.world/haskell#Pb_gPCQuqY7r77v1IHH_vWg)

What's more, Haskell's Num abstraction lets you defer deciding what type to use as long as possible.

-- Define a function to make big numbers.  The (inferred) type is generic.
Prelude> superbig n = 2 ^ 2 ^ n

-- We can call this function with different concrete types and get different results.
Prelude> superbig 5 :: Int
4294967296
Prelude> superbig 5 :: Float
4.2949673e9

-- The `Int` type is not arbitrary precision, and we might overflow.
Prelude> superbig 6 :: Int
0

-- `Double` can hold bigger numbers.
Prelude> superbig 6 :: Double
1.8446744073709552e19
Prelude> superbig 9 :: Double
1.3407807929942597e154

-- But it is also not arbitrary precision, and can still overflow.
Prelude> superbig 10 :: Double
Infinity

-- The Integer type is arbitrary-precision though, and can go as big as we have memory for and patience to wait for the result.
Prelude> superbig 12 :: Integer
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336

-- If we don't specify a type, Haskell will infer one with arbitrary precision.
Prelude> superbig 12
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336
jbyler
  • 6,013
  • 3
  • 27
  • 36