-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Inconsistent behavior when using float on complex number vs complex quantity #10010
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
@namurphy - that is an annoying inconsistency, though it originates with numpy, not astropy:
So, if we were to make Arguably, best would be for numpy to fix this (if they want to fix it; it is not completely obvious what the right behaviour is). Might it make sense to raise a numpy issue? |
If you open a Numpy issue, please cross-link to this issue. Thank you! |
Thanks! There was already a similar issue that I replied to: numpy/numpy#13007 I suppose we could close this since it's an upstream issue with NumPy, though another possibility would be adding it at the end of the list of known issues in the docs. At the very least, people in the future who run into This issue brought to you by writing tests for edge cases that will probably never happen... 😹 |
@namurphy , I think documenting it in known issues is a great idea. Would you be interested to open a PR? 😄 |
I just volunteered for something again, didn't I? 🙀 |
The PR was just merged, so it's now in the docs as a known issue. |
Thanks, @namurphy - Since I think this is more or less the best we can do, I'll close this issue. |
Description
Using
float
ornumpy.float
on a complex number results in aTypeError
. Usingfloat
ornumpy.float
on that complex number represented as a dimensionlessQuantity
results in the real part being returned and aComplexWarning
being issued. The consequence is that code using complex numbers and complex quantities may behave differently, so it may be necessary to write separate code to handle complex numbers and complex quantities. Code that relies on the current behavior for quantities should instead usenumpy.real
.Expected behavior
I would expect that using
float
ornumpy.float
on a complex number represented as a dimensionlessQuantity
would raise aTypeError
also.Actual behavior
Using
float
on a complex number multiplied withastropy.units.dimensionless_unscaled
returns the real part while issuing aComplexWarning
.Steps to Reproduce
The
TypeError
came from this line in quantity.py.System Details
The text was updated successfully, but these errors were encountered: