Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle arbitrarily large integers

I'm working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the largest I can get is factorial(12). What are some techniques for handling integers of an arbitrary maximum size. The language currently works by translating code to C++.

like image 517
Alex Gaynor Avatar asked Nov 21 '08 21:11

Alex Gaynor


People also ask

What is arbitrarily large number?

Arbitrary numbers are numbers that have no special property, other than being large-valued. Informally, one can consider them as adversarially-chosen numbers. Intuitively, it would not seem that arbitrary numbers offer any value.

How do you handle large integers in C++?

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications.

How does python handle large integers?

In Python, there is no integer overflow problem; therefore, Python programmers do not need to worry about what variable type to use for each integer. Python allows programmers to manipulate huge numbers without having to worry about precision loss.

What is Bigint in python?

Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate. In Python 3.0+, the int type has been dropped completely.


2 Answers

If you need larger than 32-bits you could consider using 64-bit integers (long long), or use or write an arbitrary precision math library, e.g. GNU MP.

like image 181
Barry Kelly Avatar answered Sep 24 '22 01:09

Barry Kelly


If you want to roll your own arbitrary precision library, see Knuth's Seminumerical Algorithms, volume 2 of his magnum opus.

like image 38
John D. Cook Avatar answered Sep 23 '22 01:09

John D. Cook