[英]Remainders with fractional divisions not working in Python
Remainders with fractional divisions not working in Python. 具有小数除法的余数不适用于Python。 For example,
例如,
>>> 59.28%3.12
3.119999999999999
>>> 59.28/3.12
19.0
Is there any way to get 0.0 as the output of 59.28%3.12 有没有办法让0.0作为59.28%3.12的输出
I don't know why, I don't know details of modulo implementation for floats, however this works fine: 我不知道为什么,我不知道浮点数的模实现的细节,但是这样可以正常工作:
from decimal import Decimal
Decimal("59.28") % Decimal("3.12")
EDIT: Note that you have to use quotes "
(ie strings) in constructors. Otherwise it will try to interpret both numbers as floats which is the source of the problem (incorrect approximation). 编辑:请注意,您必须在构造函数中使用引号
"
(即字符串)。否则它将尝试将两个数字解释为浮点数,这是问题的根源(不正确的近似值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.