-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
float(np.complex128) silently drops imaginary part #13007
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
Comments
Out of curiosity which numpy/python versions are you using? This should at least give you:
(or at least it does for me on 1.16.1 and python 3.7). It could be good to think about moving forward towards an error in any case. |
Huh. Actually, I also get that warning when I do it in IPython, but not inside a debugger. I'm on '1.15.4' and Python 3.6 and 3.7. In any case, the I don't know. Personally, I'm rather allergic to type-casting errors (I've seen them produce subtle, hard to detect wrong results in simulations too many times), so I would much prefer Feel free to close if you don't find this issue actionable any further. |
I think warnings should be less efficient. Note you could set them to Would have to check if there is more at work here or not. I guess we could close it, although I would agree with trying to move to an error eventually. |
I would support moving towards an error since there is inconsistent behavior with >>> import numpy as np
>>> print(np.__version__)
1.18.1
>>> standard_complex_number = 5 + 6j
>>> numpy_complex_number = np.complex128(5 + 6j)
>>> float(standard_complex_number)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
>>> np.float(standard_complex_number)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
>>> float(numpy_complex_number) # happens loudly the first time
<stdin>:1: ComplexWarning: Casting complex values to real discards the imaginary part
5.0
>>> np.float(numpy_complex_number) # happens silently the second time
5.0 I missed the |
xrefing to the astropy issue (astropy/astropy#10010), so we more likely notice when this will be closed. |
There may be two distinct cases here:
I think the first one is more obviously incorrect. I also agree that we should probably be doing less unsafe casting in the second case as well, but currently we tend to do unsafe casting everywhere in these cases. I.e. |
+1 for deprecating and then turning all of the Cc @leofang, who brought this up at data-apis/array-api#102 (comment). CuPy follows NumPy's current behaviour, but erroring out would be nicer. |
I am very open to the first case. Does anyone have a clue about whether the second case is actually used in the wild and might require work-arounds? Disabling I am happy to be convinced that a cast from complex to float can only reasonably occur in a situation dealing explicitly with complex numbers where using EDIT: To be clear, I can't think of any place where you would want to force cast everything (including complex) to float using |
Silently dropping imaginary parts is a recipe for disaster (bugs) in numerical code!
The text was updated successfully, but these errors were encountered: