当修剪由除法产生的大小数时,我得到一个溢出问题。例如:
>>> from math import floor
>>> int(100000000000000123.1)
100000000000000128
>>> floor(100000000000000123.1)
100000000000000128从类型上看,数字似乎是一个普通的浮点数,它不能以所需的精度存储。因此,即使使用floor(),我也会遇到同样的问题。有没有更好的数据类型来存储‘长浮点数/双精度浮点数’?如果是,我如何强制大整数的除法不返回一个低精度的浮点数?顺便说一句,我使用的是64位Python 3.6。
发布于 2019-04-17 23:50:48
您想要stdlib模块decimal
import decimal
long_number = '100000000000000123.1' # note that this is a string
your_decimal = decimal.Decimal(long_number)Decimal足够智能,可以从任何标准的数学运算返回Decimal对象。
result = your_decimal - 123
assert isinstance(result, decimal.Decimal)
assert your_decimal == decimal.Decimal('100000000000000000.1')https://stackoverflow.com/questions/55731508
复制相似问题