8000 BUG: comparisons between scalars and arrays with different precision · Issue #9194 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: comparisons between scalars and arrays with different precision #9194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhvk opened this issue May 31, 2017 · 3 comments
Closed

BUG: comparisons between scalars and arrays with different precision #9194

mhvk opened this issue May 31, 2017 · 3 comments

Comments

@mhvk
Copy link
Contributor
mhvk commented May 31, 2017

We've had numerous issues about float32 precision bugs, but the inconsistency in comparisons found out in #9189 (comment) seems a bit much to bear: comparing 1 and something that is slightly larger but equal to 1 at float32 precision is admittedly not that well-defined, but it does seem simplest to just convert the float32 to float64 and do the comparison there. And this is done in some cases, but not all. It seems particularly troubling that array scalars and arrays with a shape are treated differently...

# not good: float32 array with float64 array scalar (or regular scalar or python scalar)
np.array([1.], dtype=np.float32)>=np.array(1.00000001, dtype=np.float64)
# array([ True], dtype=bool)

# OK float32 array with float64 array
np.array([1.], dtype=np.float32)>=np.array([1.00000001], dtype=np.float64)
# array([False], dtype=bool)

# also OK: (array) scalar with (array) scalar
np.array(1., dtype=np.float32)>=np.array(1.00000001, dtype=np.float64)
# False
@toobaz
Copy link
toobaz commented Jul 15, 2017

Just to understand: is the following, concerning equality rather than weak inequality, the same or a different bug (assuming it is indeed a bug)?

In [2]: a = np.array([1.2], dtype='float64')

In [3]: a.astype('float32') == a[0]
Out[3]: array([ True], dtype=bool)

In [4]: a.astype('float32')[0] == a[0]
Out[4]: False

@seberg
Copy link
Member
seberg commented Jul 15, 2017

It is the same thing, though maybe a bit more surprising, the float32 scalars won't cause upcast to float64 if the other is an array. And:

n [2]: a.astype(np.float32).astype(np.float64)
Out[2]: array([ 1.20000005])

@mhvk
Copy link
Contributor Author
mhvk commented Jan 4, 2018

Closing this in favour of #10322, since there is a bit more focussed discussion there.

@mhvk mhvk closed this as completed Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0