JohnGalt
JohnGalt

Reputation: 897

Python: Huge Exponentiation Gets STUCK, but DOESN'T Raise An Error

I am using Python 3.6 on Win10 x64.

print(10**10**10**10)

Shouldn't this raise an Error? When I run this, it just doesn't print anything, but no error is raised. No OverflowError, no MemoryError, nothing. It has been running for like 10 minutes now. I assume, it is busy calculating? How can I find out, what is actually happening?

It's not finishing the script. (i.e. PyCharm is not printing exit code 0) Why is that?

More generally:

How do I catch a situation like this, if I don't know the magnitude of the calculations involved in advance? I want to stop the script from getting stuck like this "forever" without giving me any sort of error to work with.

.

SOLUTION:

As pointed out by Arthur Spoon below, the OverflowError is not raised when working with integers. If you want to be able to catch a "too large to compute in reasonable time"-error, it's easiest to just explicitly work only with floats in these circumstances.

At least this works with exponentiation since print(10**10**10.0) already throws the OverflowError.

The other option would be using something like the signal to limit the execution time of that piece of your script.

Upvotes: 2

Views: 390

Answers (2)

OLIVER.KOO
OLIVER.KOO

Reputation: 6023

It is calculating that is why it is not throwing you an error. And it will eventually give you an answer giving that if you have an infinite amount of time and space (memory) but the number you are trying to calculate is so big that your computer is definitely not going to have enough memory.

I am not sure how one can imagine how large this number that you are trying to calculate here. It is 1 followed by 10^10,000,000,000 zeros. Which is much greater than the Googol Number which is 10^100. That is a 1 with 100 zeros after it.That is a huge number!!

How big is Googol? To put that in perspective there are Estimate the total number of fundamental particles in the observable universe is 10^80 which is one followed by 80 zeros. A Googol is bigger than that. And you are trying to calculate a number that is much much much greater than Googol.

Upvotes: 0

Arthur Spoon
Arthur Spoon

Reputation: 462

The answer is in the Python documentation for OverflowError:

exception OverflowError

Raised when the result of an arithmetic operation is too large to be represented. This cannot occur for integers (which would rather raise MemoryError than give up).

So the solution is, I guess: explicitly work with float numbers. However the rest of the documentation says something about most floating point operations not being checked either... I know for a fact that a too big exp(x) will raise an OverflowError.

Edit: a solution to deal with that problem for you would probably be to raise an Exception yourself depending on the time it takes python to execute a bit of code you're interested in. I'm no expert in doing that, but you have a few possible ways of doing that here.

Upvotes: 3

Related Questions