我是 Python 的新手,在尝试使用运算符时,我遇到了这个:
>>> 7.0 / 3
2.3333333333333335
结果不应该是 2.3333333333333333 或者 2.3333333333333334。为什么要这样四舍五入?
此外,关于 Python 2.7 中的楼层划分,我的结果是:
>>> 5 / 2
2
>>> 5 // 2
2
>>> 5.0 / 2
2.5
>>> 5.0 // 2
2.0
所以我的观察是,即使在 float 的情况下,底数除法也返回整数商,而正常除法返回十进制值。这是真的吗?
最佳答案
Your language isn't broken, it's doing floating point math. Computers can only natively store integers, so they need some way of representing decimal numbers. This representation comes with some degree of inaccuracy. That's why, more often than not, .1 + .2 != .3.
关于python - 在 Python 中除以 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691774/