-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: __floordiv__
gives NaN and invalid warning for 1//0
#14900
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
Very curious about working on this. but seems a little tricky, any inputs ? @eric-wieser ? |
Ah, the issue is that |
Hey @seberg any place where we can learn the internals of this working or an overall architectural view of the system? and what is this extension *.h.src ? |
The development workflow documents describe the |
hi @seberg, i started looking more into this. is this still an issue. looks like fmod sets nan and probably causes invalid warning to be set. Tried to dig more into this and found : #7709 where it seems to indicate that this is intended behavior and not a bug. I also tried to dig into the code, to see where the invalid flag is set (either through |
Seems there are actually two things here, since we unfortunately never fixed the thing that we should return Inf and not NaN. @anirudh2290 there probably is no explicit setting, the flag probably gets set by the CPU during the modulo operation written in C (or similar). So the correct behaviour is arguably to return inf and set the "divide" warning. What we need to figure out is which warning is correct. Python returns ZeroDivisionError also for |
__floordiv__
gives NaN and invalid warning for 1//0
thanks @seberg! Currently for np.divmod , it behaves differently for scalars and arrays. for scalar it returns (0, 0) and gives a divide by 0 warning. For arrays, it gives an array of nans (for both quotient and remainder) and gives an invalid warning. Is my understanding correct, that your preference is to return (inf, nan) and set the dividebyzero warning for both scalars and arrays ?
|
I did not realize the |
I guess I was not completely accurate before. Float division for example np.divmod(1.0, 0.0) do EDIT: I meant print (nan, nan). |
Ah, thanks for making it more clear. Right for integers it is a bit more tricky, since the only other thing we could do is either always raise or return |
for c 1/0 is undefined behavior, on my compiler it gives floating point error core dumped. 1.0/0.0 returns inf, conforming to IEEE754 standard. |
This may be a stupid question but why can't we have the same behavior for int and float, that is set the div error and return inf and nan. |
okay, my bad. inf doesnt really make sense for int. |
Digging into some history, I found this : #7510 , which raises ValueError, the discussion that happened on the issue seems to have converged to this comment : #7510 (comment) . I guess we decided to incrementally fix this and raise ValueError wherever we find such cases ? |
np.power(0, -1) today:
|
The
__floordiv__
and__mod__
operators do not raise an exception for zero division withnp.errstate(divide='raise')
, instead replacing those instances withnan
as iferrstate
is in the default state.Numpy: 1.17.3
The text was updated successfully, but these errors were encountered: