Thanks to visit codestin.com
Credit goes to github.com

Skip to content

np.around and np.round supports complex numbers, but not np.trunc #17100

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

Open
xuhdev opened this issue Aug 18, 2020 · 3 comments
Open

np.around and np.round supports complex numbers, but not np.trunc #17100

xuhdev opened this issue Aug 18, 2020 · 3 comments

Comments

@xuhdev
Copy link
Contributor

xuhdev commented Aug 18, 2020

Both around and round supports complex input, but not trunc. This looks a bit abnormal.

Reproducing code example:

In [1]: import numpy as np

In [2]: a=np.array([complex(1+1j)]*3)

In [3]: np.around(a)
Out[3]: array([1.+1.j, 1.+1.j, 1.+1.j])

In [4]: np.round(a)
Out[4]: array([1.+1.j, 1.+1.j, 1.+1.j])

In [5]: np.trunc(a)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-17494546b8df> in <module>
----> 1 np.trunc(a)

TypeError: ufunc 'trunc' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Numpy/Python version information:

1.19.1 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0]
@eric-wieser
Copy link
Member

eric-wieser commented Aug 20, 2020

I think this is deliberate. math.trunc doesn't support floatscomplex in python either:

In [14]: import math

In [15]: math.trunc(1.2+3.4j)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-25c8caeb25a5> in <module>
----> 1 math.trunc(1.2+3.4j)

TypeError: type complex doesn't define __trunc__ method

I don't expect numpy to change here unless python changes first.

Edit (mattip): float -> complex

@eric-wieser eric-wieser added 57 - Close? Issues which may be closable unless discussion continued and removed 57 - Close? Issues which may be closable unless discussion continued labels Aug 20, 2020
@eric-wieser
Copy link
Member

eric-wieser commented Aug 20, 2020

Ah, I'm reading this backwards - if we care about consistency with python, we should deprecate round(complex):

In [18]: round(1+2j)
TypeError: type complex doesn't define __round__ method

So either we should add trunc, or deprecate round

@xuhdev
Copy link
Contributor Author

xuhdev commented Aug 20, 2020

Why don't we propose Python to add these support? :-)

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

2 participants