|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
是这样的,在学习round()的时候,看到帖子说round()的机制是采用了”四舍六入五看齐“的做法。当尾数为5 的时候,又采用是奇进偶舍的方法。但是当我输入round(1.15,1) 和round(1.25,1)的时候,发现一个是1.1, 一个是 1.2 .....太奇怪了..... 不是应该round(1.15,1 )输出的是1.2 的嘛
- >>> round(1.15,1)
- 1.1
- >>> round(1.25,1)
- 1.2
复制代码
本帖最后由 jackz007 于 2021-3-22 17:07 编辑
看看 Python 3.8.7 的手册上说了些什么
- Note:
- The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.
复制代码
百度翻译的译文为
- 注:
- round() 的行为可能令人惊讶:例如,round(2.675,2)给出的是2.67,而不是预期的2.68。这不是一个bug:这是因为大多数小数不能精确地表示。有关详细信息,请参阅浮点运算:问题和限制。
复制代码
毫无疑问,楼主所遭遇的正是手册上所说的所谓 "令人惊讶" 的结果。
|
|