【发布时间】:2015-02-28 21:18:18
【问题描述】:
正如预期的那样,由于其有限的精度,Python 的浮点乘法不能分配给加法:
In [10]: 200 * 0.1 + 200 * 0.2
Out[10]: 60.0
In [11]: 200 * (0.1 + 0.2)
Out[11]: 60.00000000000001
加法不是关联的:
In [12]: 1e14 + (48.18 + 18.26)
Out[12]: 100000000000066.44
In [13]: (1e14 + 48.18) + 18.26
Out[13]: 100000000000066.45
但是加法是可交换的吗?乘法?
【问题讨论】:
-
这个问题不是python特有的。
-
嗯,有这个:stackoverflow.com/questions/24442725/… 这表明浮点加法在 C 中不可交换。但下面的答案表明它在 Python 中是。
标签: python floating-point arithmetic-expressions