user2546845
user2546845

Reputation:

Storing arbitrary precision integers

I am writing a little arbitrary precision in c(I know such libraries exist, such as gmp, nut I find it more fun to write it, just to exercise myself), and I would like to know if arrays are the best way to represent very long integers, or if there is a better solution(maybe linked chains)? And secondly how does python work to have big integers?(does it use arrays or another technique ?)

Thanks in advance for any response

Upvotes: 0

Views: 1197

Answers (2)

tohava
tohava

Reputation: 5412

Try reading documentation about libgmp, it already implements bigints. From what I see, it seems like integers are implemented as a dynamically allocated which is realloc'd when the number needs to grow. See http://gmplib.org/manual/Integer-Internals.html#Integer-Internals.

Upvotes: 1

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13196

Python long integers are implemented as a simple structure with just an object header and an array of integers (which might be either 16- or 32-bit ints, depending on platform).

Code is at http://hg.python.org/cpython/file/f8942b8e6774/Include/longintrepr.h

Upvotes: 0

Related Questions