Why does Python have such big numbers?

Asked

Viewed 112 times

4

What would be the largest number in python? Python shows the result of expressions like:

>>> 8**150000

The result was 135465 digits! I wanted to know the language can do this.

  • 4

    https://www.reddit.com/r/Python/comments/2bbbvk/how_does_python_handle_bignum_arithmetic/

1 answer

5


Python is a language of script, so what matters to her is to be easy, is to deliver what the programmer asks without creating difficulties. No matter efficiency or any other aspect.

The computer needs certain space to represent types because memory is a storage location with bounded space. It’s physical, in order to put something somewhere you need to know the size to be reserved.

To be efficient a language needs to have types that take up little space because most operations require very little. At the same time it needs to have different types that take up a lot of space because some operations need very large data.

When efficiency doesn’t matter, one of the possible techniques is to only have a type that takes up a lot of space and can store large numbers, even if it doesn’t use most of the time.

But as much space as you reserve can still be missing in certain situations, and the booking of something very large can make it impossible to use, not only make it inefficient. So you need another technique that accepts small and large numbers in a flexible way.

In the same way that Python created an infrastructure to deal with variables which may have data of different types, although this is not possible directly on the computer, the language has also created a way for the number to accept arbitrary sizes. There is a mechanism that controls this and increases the size of the possible number as you need it. Obviously this has a overhead memory and processing, but it doesn’t matter to the language. This is with some form of indirect.

While in many languages typing is efficient and simple, just like the computer needs, forcing the programmer to deal with the right type according to what he needs, Python has something complex internally to facilitate the life of the programmer.

In fact other languages usually have in their library a similar infra to handle arbitrary size numbers, but you only use if you need, in Python you pay the price of it always.

Mechanism

The most commonly used technique to achieve this is similar to a array, or a string, which is still a array. The number is broken down into parts, then each element of the array is being one of the parties.

Let’s say the internal structure was a array bytes (can use something larger for more efficiency, I would use a int or long), Up to 256 integers need only 1 byte (plus the extra cost to control size and possibly other issues), if up to 65536 needs 2 bytes, up to about 16 million needs 3 bytes, and 4 billion needs 4, and around 1 trillion needs 5, and so on. And it will handle if it has signal, without decimal part.

All mathematical operations consider this complex construction and look at it all when you do the math.

You should imagine that if the number changes and the size is also necessary to copy it to another place to fit the new number, but as there is an indirect, the access is usually done without difficulties, just has a further processing.

Completion

One of the secrets of programming is creating abstractions on top of complex things. You use the simple while you have all the machinery under the hood. Nothing wrong with that in certain scenarios, but when one doesn’t understand these things one ends up creating a horde of pseudo programmers.

If you are interested you can see the source code that takes care of it all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.