5

Can you recommend good languages to do math with large numbers in?

So far I've used Actionscript 2 and Objective-c and with Objective-c even using NSDecimalNumbers I was limited to 32 digits in my calculations... I would need at a minimum to be able to calculate with numbers fifty-thousand digits long.

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • See also http://stackoverflow.com/questions/4773603/high-precision-arithmetric-in-python-and-or-c-c. – lhf Feb 06 '13 at 09:47

5 Answers5

10

Python has arbitrary-length integers and uses them transparently, so you don't need any special code or classes for this.

>>> len(str(math.factorial(123456)))
574965
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Nice. What about arrays? Are their indices limited by int.MAX_VALUE? – juanmf Jun 16 '22 at 01:13
  • 1
    If you look at `help(sys)`, you'll see that `sys.maxsize` is "the largest supported length of containers". On my system, that's equal to 2**63-1, but the actual upper bound will be much lower, a) because even on a 64 bit system, only up to 52 bits can be used to address memory, so 4 PB, and b) because containers in Python have a lot of overhead, so your lists are not comparable to, say, a C array. – Tim Pietzcker Jun 17 '22 at 19:43
4

Perhaps Haskell will appeal to you.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
4

Try also bc, which is probably already installed in your machine.

lhf
  • 70,581
  • 9
  • 108
  • 149
2

Nowadays most languages have some kind of support for arbitrary length numbers, natively on the language or via some external library (i.e. gmp).

The only important difference* is the level of integration within the language. For instance, in C++, Python, Perl, SWI-Prolog, Haskell, C#, etc., big-ints can be manipulated as any other built-in numeric type using the standard math operators. On the other hand in languages not supporting operator overloading as C, Objective-C, Java, etc. you have to use the library functions explicitly.

Depending on the pervasiveness of big-int operations on your application it may pay off to switch to a more big-int friendly language or not.

update

[*] well, obviously, correctness and speed also matter. But, as most languages use GMP under the hood, there shouldn't be mayor differences in that regard. Maybe math-oriented (and expensive!) languages/applications as Mathematica or Maple providing their own big-int implementations can have some advantage here.

salva
  • 9,943
  • 4
  • 29
  • 57
1

Try java with its BigInteger Class or you can look at writing a small library in C. If the Math is fairly simple you can always use arrays.

Perhaps try Matlab (not sure)

Techmonk
  • 1,459
  • 12
  • 20
  • 3
    Matlab, like many other choices, relies on libraries or other extensions for arbitrary-precision calculations, they're not built in. The quality of such libraries varies. – High Performance Mark Feb 06 '13 at 08:21
  • MATLAB does have a big integer tool, VPI, my own. I've since written a new one, based on the Java library that is much faster. –  Feb 06 '13 at 16:26
  • Java can take several hours to create a single large BigInteger - this is a race that Java will never win. – Kevin Aug 29 '18 at 06:51