python - 对于浮点 x,x==numpy.linspace(x,y,n)[0] 总是 True 吗?

标签 python numpy floating-point

请注意,我正在询问 numpy.linspace 的端点。这些函数保证返回一个包含端点的数组。但是端点是否保证是 float 与提供给函数的参数相同?

询问原因 - 纯粹的好奇心。除非有人能想出一个充分的理由来依赖这种行为? 谢谢。

最佳答案

这是 definition of np.linspace 的摘录:

def linspace(start, stop, num=50, endpoint=True, retstep=False):
    ...
    if endpoint:
        if num == 1:
            return array([float(start)])
        step = (stop-start)/float((num-1))
        y = _nx.arange(0, num) * step + start   #<-- the first point is `start`
        y[-1] = stop   # <-- the last point is `stop`
    ...
    return y

所以,是的,当 endpoints 为 True 时,返回的端点将完全等于 startstop


请注意,即使 endpoints=True(默认情况下),如果 num 是,np.linspace 也可能不会返回端点小于2:

In [8]: np.linspace(0, 1, num=0)
Out[8]: array([], dtype=float64)

In [9]: np.linspace(0, 1, num=1)
Out[9]: array([ 0.])

关于python - 对于浮点 x,x==numpy.linspace(x,y,n)[0] 总是 True 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821848/

相关文章:

c++ - 整数 float 除以自身是否保证为 1.f?

java - 分类和回归树 - 用 GINI 进行最优分割计算

python - 从特定日期的 pandas DataFrame 中选择行

python - 在 Python 中使用 multiprocessing.Array 时出现 OSError (Errno 9)

python - 使用 Matplotlib 的 pyplot 绘制分隔 2 个类的决策边界

Python。积分问题。类型错误 : only size-1 arrays can be converted to Python scalars

python - 合并numpy中的行以形成新数组

math - float 学有问题吗?

Python-sphinx:忽略 .rst 文件中的文本

java - java中的向下舍入 float 转换