2

I read something here

I was wondering how is python able to handle such large values. During computation where are they stored? After all, the CPU has to compute the values, so how can a CPU handle values larger than the sys.maxint? Why is that languages that support fixed length values unable to handle such larger numbers? What is python's trick?

Note: I am confused how is the CPU able to handle the computation of larger values and where these values are stored.

Community
  • 1
  • 1
codingsplash
  • 4,785
  • 12
  • 51
  • 90
  • Regarding "other languages": there're [GMP](https://gmplib.org/) and other libraries for C, see http://stackoverflow.com/questions/1055661/bigint-bigbit-library – user3159253 Nov 19 '15 at 06:28
  • The basic idea for any library for integers larger than the largest integer for a given platform is to split the math into pieces and perform separate calculations (similar to how you do the classical "long multiplication") and then keep results as a set of numbers (e.g. a structure of lower and higher integer). – user3159253 Nov 19 '15 at 06:35
  • Well, likely it's the complete answer :) – user3159253 Nov 19 '15 at 06:36
  • Well, I am aware that python converts it into bignum, from the question I had linked. I am also aware that bignum is handled as 'long multiplication'. But my question specifically was how is the CPU able to handle larger values, as eventually even by long multiplication at some point the value will be larger that what the CPU can handle. Where is that large number stored? – codingsplash Nov 19 '15 at 06:41
  • Well, I've explained this already: CPU doesn't handle bignums. Instead it handles a _set_ of smaller intergers, each of which doesn't exceed the platform limit and in place of a single multiplication or division a series of operations is performed similar (but not completely identical, of course) to long multiplication. The result is still represented as a set of regular integers, and is concatenated only when python forms a string representation of the number when e.g. printing it to console. – user3159253 Nov 19 '15 at 06:45
  • Certainly handling such a set implies performance penalties. – user3159253 Nov 19 '15 at 06:46
  • Cool! now I get it! If you can elaborate the penalties a bit and add your earlier info into a separate answer, I will gladly accept the answer. Thanks! – codingsplash Nov 19 '15 at 06:49
  • Likely I can't since the question has been approved already as a duplicate. Never mind. – user3159253 Nov 19 '15 at 06:52

0 Answers0