Python十进制模块 - 不想要的类浮点输出? [英] Python Decimal Module - Undesired Float-Like Output?
本文介绍了Python十进制模块 - 不想要的类浮点输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
y
不是== 0的两个值?我认为十进制模块的全部要点是摆脱浮尘......下面是一个数学例程的极简化版本,它将数字作为变量传递。 from decimal import *
getcontext()。prec = 2
q = Decimal 0.01)
x =十进制(0.10)*十进制(0.10)
$ b $ =十进制(x) - 十进制(q)
print(x, y,Decimal(y))
'''
x == 0.010
y == -2.1E-19
十进制(y)== -2.1E- 19
'''
解决方案
数字作为字符串
>>>十进制('0.10')*十进制('0.10') - 十进制('0.0100')
>>>十进制('0.000')
This I imagine is extremely simple - but why in the following are the two values for y
not == 0? I thought the whole point of the decimal module was to get rid of the float dust ...The following is an extremely simplified version of a mathematical routine that passes numbers around as variables.
from decimal import *
getcontext().prec = 2
q = Decimal(0.01)
x = Decimal(0.10) * Decimal(0.10)
y = Decimal(x) - Decimal(q)
print(x,y, Decimal(y))
'''
x== 0.010
y== -2.1E-19
Decimal(y) == -2.1E-19
'''
解决方案
Try specifying the numbers as strings
>>> Decimal('0.10') * Decimal('0.10') - Decimal('0.0100')
>>> Decimal('0.000')
这篇关于Python十进制模块 - 不想要的类浮点输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文