IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python3中round的迷惑行为 -> 正文阅读

[Python知识库]python3中round的迷惑行为

在用round的过程中遇到过两个比较迷惑的问题,记录一下。

1、round是用来实现浮点数的近似舍入的,一般而言四舍五入法则深入人心,但是这个round的行为并不完全符合四舍五入,而是四舍六入五成双。关于这种舍入方法PEP327中有详细的说明。

https://www.python.org/dev/peps/pep-0327/#decimal-methods

round-half-even: If the discarded digits represent greater than half (0.5) then the result coefficient is incremented by 1; if they represent less than half, then the result is not adjusted; otherwise the result is unaltered if its rightmost digit is even, or incremented by 1 if its rightmost digit is odd (to make an even digit)

2、round方法文档中说:This returns an int when called with one argument,然而实际上返回的并不一定是int,后面会详细描述,这里不注意可能会导致代码报错。

import numpy as np
import sys

print("python version: ", sys.version)
print("numpy version: ", np.__version__)

print("round(10.5) ->", round(10.5))
print("round(-10.5) ->", round(-10.5))
print("round(11.5) ->", round(11.5))
print("round(-11.5) ->", round(-11.5))
print("round(9.51,1) ->", round(9.51, 1))
print("round(9.55,1) ->", round(9.55, 1))
print("round(9.56,1) ->", round(9.56, 1))
print("round(8.51,1) ->", round(8.51, 1))
print("round(8.55,1) ->", round(8.55, 1))
print("round(8.56,1) ->", round(8.56, 1))
print("type(round(7.7)) ->", type(round(7.7)))
print("type(round(np.float(7.7))) ->", type(round(np.float(7.7))))
print("type(round(np.float32(7.7))) ->", type(round(np.float32(7.7))))
print("type(round(np.float64(7.7))) ->", type(round(np.float64(7.7))))

?运行结果如下:

python version: ?3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)]
numpy version: ?1.14.5
round(10.5) -> 10
round(-10.5) -> -10
round(11.5) -> 12
round(-11.5) -> -12
round(9.51,1) -> 9.5
round(9.55,1) -> 9.6
round(9.56,1) -> 9.6
round(8.51,1) -> 8.5
round(8.55,1) -> 8.6
round(8.56,1) -> 8.6
type(round(7.7)) -> <class 'int'>
type(round(np.float(7.7))) -> <class 'int'>
type(round(np.float32(7.7))) -> <class 'numpy.float32'>
type(round(np.float64(7.7))) -> <class 'numpy.float64'>

总结1:round计算规律

可以看到,python版本是3.6.6,numpy版本是1.14.5?。很明显四舍六入是没问题的,五成双的意思是当保留精度的那一位的后一位是5的情况下,保留精度这一位如果是偶数则不变,是奇数则向上取偶数。

比如round(10.5)保留到整数部分的个位,那么保留精度的下一位是十分位5,各位是偶数0不变,所以round(10.5)结果是10.5。比如round(11.5)的各位是奇数1,所以向上取12。

据说这种舍入规则是为了保证数据的精确性,但是却将简单的四舍五入实现变得不那么简单,至少要实现精确的四舍五入就不得不加一些判断(这可能让review代码的人感到不明觉厉)。

总结2:numpy不同版本对round结果type的影响

上面计算中使用的numpy版本是1.14.5,结果中的type(round(np.float32(7.7))) 输出为 <class 'numpy.float32'>

将numpy版本改为1.19.5,重新计算结果如下:

python version: ?3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)]
numpy version: ?1.19.5
round(10.5) -> 10
round(-10.5) -> -10
round(11.5) -> 12
round(-11.5) -> -12
round(9.51,1) -> 9.5
round(9.55,1) -> 9.6
round(9.56,1) -> 9.6
round(8.51,1) -> 8.5
round(8.55,1) -> 8.6
round(8.56,1) -> 8.6
type(round(7.7)) -> <class 'int'>
type(round(np.float(7.7))) -> <class 'int'>
type(round(np.float32(7.7))) -> <class 'int'>
type(round(np.float64(7.7))) -> <class 'int'>

可以看到type(round(np.float32(7.7)))的输出结果为<class 'int'>,也就是说在使用numpy1.14.5的时候,round(np.float32(7.7))返回的数值并不是int型,所以不能作为索引值,否则会出现类似下面这样的错误。

TypeError: list indices must be integers or slices

但是在numpy1.19.5中不存在这个问题,可以将round(np.float32(7.7))作为索引值。目前不知道具体哪个版本改了这个地方,取索引的时候还是int强转比较稳妥,int(round(np.float32(7.7)))

np.float32(7.7)这种写法实际中不会出现的,一般是np.sin()之类的方法会返回一个np.float32类型的结果。

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-04 19:27:40  更:2021-07-04 19:28:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 22:48:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码