0

Why do the below three lines produce different output in python3, despite the values being equal?

>>> 10 ** 500
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

>>>1E500
inf

>>>1E250 ** 2
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-110-d71dbae32ab9> in <module>
----> 1 1E250**2
0

1 Answer 1

3

They are not equal. The first is an integer, and Python allows integers to be infinitely large. (Well, virtually infinite.) The other two are floating point numbers, and the value is beyond the range of a double. The 1E500 value is a constant, and overflows in constants result in the inf pseudo-value. The final one is an arithmetic operation, and Python detects and throws overflows in arithmetic operations.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. Why is it that the values are not equal? I see that these numbers are of different types but the numerical values are equivalent at least in a mathematical sense (i.e. 10 == 10.0).
The world of floating point numbers is a strange and magical one. They are stored in binary, so it's actually N x 2**M, and you only have 54 bits available for N. So, except for a rather narrow list of small integers, floating point values are always approximations. Try doing int(1E100) to see that. That's the closest multiple of a power of 2 you can get, but it only has 17 valid digits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.