Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase Accuracy of float division (python)

Tags:

python

pycharm

I'm writing a bit of code in PyCharm, and I want the division to be much more accurate than it currently is (40-50 numbers instead of about 15). How Can I accomplish this?

Thanks.

like image 756
louie mcconnell Avatar asked Jun 09 '14 02:06

louie mcconnell


1 Answers

Check out the decimal module:

>>> from decimal import *
>>> getcontext().prec = 50
>>> Decimal(1)/Decimal(7)
Decimal('0.14285714285714285714285714285714285714285714285714')

If you're interested in more sophisticated operations than decimal provides, you can also look at libraries like bigfloat, or mpmath (which I use, and like a lot.)

like image 66
DSM Avatar answered Oct 19 '22 19:10

DSM