-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
BUG: Add promoter to ldexp
for python integers to prevent overflow
#29597
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
base: main
Are you sure you want to change the base?
Conversation
What will happen with complex input? |
@mattip this only promotes integer inputs (Python scalar integer, actually), so the rest would stay the same? |
Thanks a for looking at this, this is very cool to see this use! I am curious if we can push this further and actually use the promoter always. That is, we:
|
Thanks, that extension makes a lot of sense! I tried implementing this, but it causes a segmentation fault. It seems to be due to the fact that Edit: that does seem to be it; reverting the operation for the first dtype and setting the others in this way doesn't crash (though of course the promotion doesn't work properly) |
Oh, hmmm, the pyfloat one should work ok probably, although the abstract version woukd be nicer. |
|
Making the abstract and PyFloat do the same thing probably makes sense, but the fact that it doesn't work here seems like a bug somewhere that I (or we) need track down to make this work (and a couple of other cases, because we should use this for other float only functions). |
To be clear it doesn't crash, it just doesn't promote further up than In line 223-226 of abstractdtypes.c we do in else if (other == &PyArray_PyLongDType) {
Py_INCREF(cls);
return cls;
} This is equivalent to doing no promotion as we already know it's a float, but looking at NEP 50, this actually seems correct behavior - Python ints can promote to the lowest float (given we need a float) right? So for |
Yes, we promote to numpy/numpy/_core/src/multiarray/abstractdtypes.c Lines 217 to 226 in 583993f
|
Ah, that makes sense (if I am getting the right picture). We are getting the abstract version as a result. In other paths that is OK because we only need a concrete dtype instance (via default_descr), but here we need to convert the abstract one to the concrete Float64 one. |
Fixes #29182.
Adds a promoter for first argument
PyArray_PyLongDType
toldexp
. It casts straight todouble
, not sure if that is more arbitrary than needed.Thanks for reviewing!