If your accuracy requirement is 100 decimal digits, I think you need to use decimal.Decimal .
Python float is not designed for this kind of exact calculation.
Using decimal.Decimal almost as simple as float , you can do + , - , * , / and some other commonly used calculations directly between any decimal.Decimal without any consideration.
In addition, decimal.Decimal supports the adjustment of the directly required precision:
>>> from decimal import getcontext, Decimal >>> getcontext().prec = 6 >>> Decimal(1) / Decimal(7) Decimal('0.142857') >>> getcontext().prec = 28 >>> Decimal(1) / Decimal(7) Decimal('0.1428571428571428571428571429')
You can find more information in this Python2 API or Python3 API
source share