You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why does indexing an array after boolean comparison provide different results than doing a boolean comparison of an index? #10982
Why is the first comparison getting it wrong? And how can I fix it?
The values:
arr[3154950]= 3.1415927
np.pi= 3.141592653589793
Is the problem with precision?
Would someone be able to explain the difference in the way numpy approaches the problem? It looks like using np.float64 explicitly fixes the problem, but I'm curious to know if numpy does a different casting in the two indexing methods.
@eric-wieser ah perfect. That's exactly what I was looking for. So in the first example numpy treats the values of the arr as np.float32 to match np.pi correct?
This question has been answered, and links to relevant bug reports are made, so closing the issue. Please reopen if you think it tracks something distinct not duplicated in another issue.
Activity
eric-wieser commentedon Apr 26, 2018
Numpy has different promotion rules for scalars vs arrays, which I'd consider to be a bug (#10322)
In this case,
arris of dtypenp.float32, andnp.piis of dtype np.float64.Your first example is computing
arr > np.float32(np.pi), whereas your second is computingnp.float64(arr[3154950]) > np.pigrantmwilliams commentedon Apr 26, 2018
@eric-wieser ah perfect. That's exactly what I was looking for. So in the first example numpy treats the values of the arr as np.float32 to match np.pi correct?
eric-wieser commentedon Apr 26, 2018
You have that backwards. In the first example numpy throws away precision from np.pi first, casting it from float64 to float32
grantmwilliams commentedon Apr 26, 2018
Oh right that's totally my bad. Guess i was reading too fast
bsipocz commentedon May 21, 2022
This question has been answered, and links to relevant bug reports are made, so closing the issue. Please reopen if you think it tracks something distinct not duplicated in another issue.