Skip to content
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

numpy.linalg.norm returns nan for an array of int16 #6128

Closed
WarrenWeckesser opened this issue Jul 28, 2015 · 4 comments
Closed

numpy.linalg.norm returns nan for an array of int16 #6128

WarrenWeckesser opened this issue Jul 28, 2015 · 4 comments

Comments

@WarrenWeckesser
Copy link
Member

Example:

In [1]: np.__version__
Out[1]: '1.10.0.dev0+a92c4a1'

In [2]: x = np.array([ -91, -119, -137, -132], dtype=np.int16)

In [3]: np.linalg.norm(x)
/Users/warren/local_scipy/lib/python2.7/site-packages/numpy/linalg/linalg.py:2126: RuntimeWarning: invalid value encountered in sqrt
  ret = sqrt(sqnorm)
Out[3]: nan

The example is from http://stackoverflow.com/questions/31685156/normalizing-vector-produces-nan-in-numpy, although it is not clear in that question if the array has type int16.

@WarrenWeckesser
Copy link
Member Author

Possibly related issues:
#5626
#4851

@argriffing
Copy link
Contributor

I think the explanation is #4126, so for example

>>> x = np.array([ -91, -119, -137, -132], dtype=np.int16)
>>> x.dot(x)
-6901
>>> (x*x).sum()
58635

@jakirkham
Copy link
Contributor

So, this should also be helped by @charris's proposal ( #5626 (comment) ) to converting everything to floats or similar before computing.

@WarrenWeckesser
Copy link
Member Author

Fixed by #7088.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants