In python, integers have arbitrary precision and therefore we can represent an arbitrarily large range of integers (only limited by the available memory).
In [1]:
x = 7**273
print(x)
print(type(x))
Python uses (hardware) 754 double precision representation for floats. This means that some floats can be only represented approximately.
0.1
:
In [2]:
format(0.1, '.80f')
Out[2]:
In [3]:
.1 + .1 + .1 == .3
Out[3]:
In [4]:
.1 + .1 == .2
Out[4]:
In [5]:
from decimal import Decimal, getcontext
In [6]:
getcontext().prec=80
format(Decimal(1)/Decimal(7), '.80f')
Out[6]:
In [7]:
format(1/7, '.80f')
Out[7]:
In [8]:
#12345678901234567 (17 digits)
In [9]:
Decimal(1)/Decimal(7)
Out[9]:
format
:
In [10]:
print('{:.50f}'.format(Decimal(1)/Decimal(7)))
In [11]:
# https://stackoverflow.com/questions/28284996/python-pi-calculation
from decimal import Decimal, getcontext
getcontext().prec=1000
my_pi= sum(1/Decimal(16)**k *
(Decimal(4)/(8*k+1) -
Decimal(2)/(8*k+4) -
Decimal(1)/(8*k+5) -
Decimal(1)/(8*k+6)) for k in range(1000))
'{:.1000f}'.format(my_pi)
Out[11]:
You can visit 100,000 Digits of Pi or One Million Digits of Pi to check the correctness this code.