python numpy arange意外结果 [英] python numpy arange unexpected results

查看:62
本文介绍了python numpy arange意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用arange函数来定义for循环迭代并获得意外结果.

I am using the arange function to define my for loop iterations and getting unexpected results.

i = arange(7.8,8.4,0.05)
print i

满足以下条件:

[ 7.8   7.85  7.9   7.95  8.    8.05  8.1   8.15  8.2   8.25  8.3   8.35 8.4 ]

但是使用的终止值为8.35如下

yet using the stop value of 8.35 as follows

i = arange(7.8,8.35,0.05)

产生以下结果

[ 7.8   7.85  7.9   7.95  8.    8.05  8.1   8.15  8.2   8.25  8.3 ]

但是我希望我的射程结束于8.35!我知道我可以使用大于8.35且小于< 8.4达到我的结果,但是为什么会有所不同并且在我看来却不一致?

But I want my range to end at 8.35! I know I can use the stop value of > 8.35 and < 8.4 to achieve my result, but why is it different and in my mind, inconsistent?

我正在使用2.7版

推荐答案

也许与浮点数的限制有关.由于机器的精度,不可能将所有可能的值完美地存储为浮点数.例如:

Perhaps it has to do with limitations on floating point numbers. Due to machine precision, it is not possible to store every conceivable value perfectly as a floating point. For example:

>>> 8.4
8.4000000000000004
>>> 8.35
8.3499999999999996

因此,作为浮点数的8.4略大于8.4的实际值,而作为浮点数的8.35则稍小一点.

So, 8.4 as a floating point is slightly greater than the actual value of 8.4, while 8.35 as a floating point is a tiny bit less.

这篇关于python numpy arange意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆