有人可以用 来解释这个舍入问题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
似乎很无辜。. . 这个四舍五入的问题会不会让我们到处都感到困惑?