Tomasz Sikora
Tomasz Sikora

Reputation: 1767

Why is 10 to the power not equal to scientific notation for large numbers in Python?

Why is 10**5 equal to 1e5 but 10**50 is not equal to 1e50 in Python?

Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10**5 == 1e5
True
>>> 10**50 == 1e50
False

It's true up to 10**22. Then it's false:

>>> 10**22 == 1e22
True
>>> 10**23 == 1e23
False

Upvotes: 5

Views: 606

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70287

Python 3 supports big integers and uses them whenever it can. 10**50 is a calculation on integers and produces the exact number ten to the fiftieth power. On the other hand, scientific notation always uses floating point, so 1e50 is a floating-point value that's approximately equal to ten to the fiftieth power.

>>> type(10 ** 50)
<class 'int'>
>>> type(1e50)
<class 'float'>

Upvotes: 13

Related Questions