有人能解释一下关于numpy.linspace的取整问题吗?
import numpy as np
np.linspace(0, 1, 6) == np.around( np.linspace(0, 1, 6), 10 )
# array([ True, True, True, False, True, True], dtype=bool)
以下是我到达这里的方式...
import numpy as np
## Two ways of defining the same thing
A = np.array([ 0., 0.2, 0.4, 0.6, 0.8, 1. ])
B = np.linspace(0, 1, 6)
## A and B appear to be the same
A # array([ 0., 0.2, 0.4, 0.6, 0.8, 1. ])
B # array([ 0., 0.2, 0.4, 0.6, 0.8, 1. ])
## They're not
A == B # array([ True, True, True, False, True, True], dtype=bool)
A - B # array([ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -1.11022302e-16, 0.00000000e+00, 0.00000000e+00])
## Gotta round to get my expected result
C = np.round( np.linspace( 0, 1, 6 ), 10 )
C # array([ 0., 0.2, 0.4, 0.6, 0.8, 1. ])
A == C # array([ True, True, True, True, True, True], dtype=bool)
我定义的B
似乎很无害……这个舍入问题会在许多地方影响我们吗?