Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integers of unlimited size?

In Python, I can write a program to calculate integers of unlimited size. Just the other day I did the one millionth fibonacci number, and it was so large It was unable to fit in the console. If this is possible to do in Python, which to my understanding was written in C, how could one do the same in C++? It has to be possible, otherwise I don't see how it could be done in Python. I also believe there is something similar in Java/C# called a Biginteger, but I couldn't find anything saying how it could be implemented in C++.

like image 360
WillumMaguire Avatar asked Sep 29 '12 01:09

WillumMaguire


People also ask

How big is a Python integer?

These represent numbers in the range -2147483648 through 2147483647. (The range may be larger on machines with a larger natural word size, but not smaller.)

What are the 3 types of numbers in Python?

Numeric Types — int , float , complex. There are three distinct numeric types: integers, floating point numbers, and complex numbers.

How large can an integer be in Python 3?

Integers in Python 3 are of unlimited size. Python 2 has two integer types - int and long.

What is integer in Python example?

In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10.


2 Answers

Anything is possible in C/C++. You could write you own class called bigInteger and use an array to represent the number, but the best way is to use an already written library for working with big numbers, because those are very well optimized and a huge part of them is written in asm for extra speed. Here are a few examples:

  • GMP (it provides also a C++ interface)

  • http://www.imach.uran.ru/cbignum/

like image 151
Ionut Hulub Avatar answered Sep 27 '22 23:09

Ionut Hulub


Wikipedia lists a number of libraries you can use. Those that are denoted as C libraries can also be used in C++. C++ does not have a built-in bigint type.

As this reference shows, it's generally a good idea to google, check wikipedia and not the least, check the FAQ, before asking.

However, in this case, inexplicably, it seems that the FAQ does not provide an answer.

like image 37
Cheers and hth. - Alf Avatar answered Sep 27 '22 22:09

Cheers and hth. - Alf