2

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.

JohnGalt
  • 797
  • 1
  • 9
  • 21
  • 1
    Why is that? Because it is still calculating. Have you any idea how large that number will be? – Willem Van Onsem Jul 30 '17 at 18:06
  • 1
    Python uses arbitrary precision integers afaik. It's probably actually trying to calculate that, and eating up all the memory in your computer in the process. Open up your Task Manager, find the process, and see what's it's doing. Or use a profiler. – Carcigenicate Jul 30 '17 at 18:06
  • @Willem Van Onsem : Yes, that's exactly why I tried that. I expected an Error to be raised because of the magnitude. – JohnGalt Jul 30 '17 at 18:13
  • Give it some time. Either Python will raise a `MemoryError`, or Python will have eaten up to much memory trying to do the calculation, your computer will freeze up, and you'll have to reboot. – Christian Dean Jul 30 '17 at 18:14
  • @Carcigenicate That was a great tip! I did look at it in the Task Manager. The memory usage is actually increasing noticably over time, while the CPU usage is roughly constant. So it actually is still calculating! – JohnGalt Jul 30 '17 at 18:31

2 Answers2

2

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.

Community
  • 1
  • 1
Arthur Spoon
  • 442
  • 5
  • 18
  • Thanks! Using floats actually does the trick! (i.e. throws the OverflowError) At least when doing exponentiation. And it does so if either number is a float, by the way. Still, I'm going to read the stuff on execution time dependent Exceptions. That should come in handy. – JohnGalt Jul 30 '17 at 18:36
0

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.

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
  • Again, I am aware of the magnitude of the number, thank you. (And it is actually MUCH larger than a 1 with 1000 zeros. It's a 1 with 10^10^10 zeroes, or in other words a 1 with 10^10,000,000,000 zeros.) The question was, why there was **no error** raised. – JohnGalt Jul 30 '17 at 18:40
  • you are right about the number of zeros but I did answer you why there was no error raised. Because it is still processing. – OLIVER.KOO Jul 30 '17 at 18:49