python - 在 Python 中除以 3

标签 python division floor

我是 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 的情况下,底数除法也返回整数商,而正常除法返回十进制值。这是真的吗?

最佳答案

看看这个0.30000000000000004.com

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/

相关文章:

python - Numpy 鸿沟和 Python 鸿沟之间的区别?

c++ - round 比 LLVM 中的 floor/ceil/int 慢得多

database - 如何在 Flutter Floor 中实际查看数据库?

java - 为什么当我尝试在 try block 内进行类型转换时,我的 java 代码会失败?

java - 如何正确地将 Java 中的 double 值设置为两位数

python - Raspberry PI 通过 URL 向 PHP 脚本发送数据

python - FFT算法错误

python - SVM 的余弦相似度内核

python - 使用 cv2.imread : "<built-in function imread> returned NULL without setting an error", 好像打不开图片或者获取数据

java - 使用递归的整数除法——以及其他一些有趣的限制