如何在Python中获得浮点类型,该类型在小数点后可以有更多数字?我用更多的数字表示大量的数字(例如几百或几千),因此十进制类型将不正确。我也尝试过GMPY2,但是我什至无法将其下载到我的Python中。是否有具有这种类型的库?
Python 3.8 | Linux Ubuntu
您可以使用十进制模块。以下是如何执行此操作的示例:
>>> import decimal
>>> decimal.getcontext().prec = 123 # Setting precision to 123 digits
>>> decimal.Decimal(10) / decimal.Decimal(3)
# Can't fit the answer here, so not showing. Try yourself
>>> decimal.getcontext().prec = 1000 # Setting precision to 1000 digits
>>> decimal.Decimal(10) / decimal.Decimal(3)
# Can't fit the answer here again, so not showing. Try yourself
这是十进制模块的文档。
请注意,根据设备上可用的内存,可以使用的最高精度可能会有所不同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句