如何在 Python 中获得更精确的浮点类型?
How to get more precise float type in Python?
如何在Python中得到一个浮点类型,它可以在小数点后有更多的数字?我所说的更多数字是指非常大的数字(例如数百或数千),因此十进制类型不正确。我也尝试使用 GMPY2,但我什至无法将其下载到我的 Python。有没有这样类型的库?
Python 3.8 | LinuxUbuntu
您可以使用十进制模块。以下是如何执行此操作的示例:
>>> 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
这是十进制模块的 documentation。
请注意,根据您设备上的可用内存,您可以使用的最高精度可能会有所不同。
如何在Python中得到一个浮点类型,它可以在小数点后有更多的数字?我所说的更多数字是指非常大的数字(例如数百或数千),因此十进制类型不正确。我也尝试使用 GMPY2,但我什至无法将其下载到我的 Python。有没有这样类型的库?
Python 3.8 | LinuxUbuntu
您可以使用十进制模块。以下是如何执行此操作的示例:
>>> 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
这是十进制模块的 documentation。
请注意,根据您设备上的可用内存,您可以使用的最高精度可能会有所不同。