3 回答

TA贡献1795条经验 获得超7个赞
也许这与浮点数的限制有关。由于机器的精度,不可能将所有可能的值完美地存储为浮点数。例如:
>>> 8.4
8.4000000000000004
>>> 8.35
8.3499999999999996
因此,作为浮点数的8.4略大于8.4的实际值,而作为浮点数的8.35则稍小一点。

TA贡献1869条经验 获得超4个赞
我猜想您正在看到浮点取整的效果。
numpy.arange与python的功能相同range:它不包含“端点”。(例如range(0, 4, 2)将[0,2]代替[0,2,4])
但是,对于浮点步长,舍入误差会累积,有时最后一个值实际上会包括端点。
如文档中所述arange:
当使用非整数步骤(例如0.1)时,结果通常将不一致。最好在linspace这些情况下使用。
numpy.linspace在起点和终点之间生成指定数量的点。顺便说一句,默认情况下确实包含端点。

TA贡献1943条经验 获得超7个赞
范围函数的帮助说
For floating point arguments, the length of the result is
``ceil((stop - start)/step)``. Because of floating point overflow,
this rule may result in the last element of `out` being greater
than `stop`.
对于python 2.7,现在在大多数平台上都可以正确舍入浮点数和字符串之间的转换。
在2.7中
>>> float(repr(2.3))
2.3
在2.6中
>>> float(repr(2.3))
2.2999999999999998
添加回答
举报