8000 BUG: Potential fix for divmod(1.0, 0.0) to raise divbyzero and return inf by anirudh2290 · Pull Request #16161 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Potential fix for divmod(1.0, 0.0) to raise divbyzero and return inf #16161

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

Merged
merged 6 commits into from
Nov 25, 2020

Conversation

anirudh2290
Copy link
Member
@anirudh2290 anirudh2290 commented May 5, 2020

This potentially fixes: #14900 . This is currently WIP until discussion concludes on #14900. According to IEEE754 standard this 1 / 0 double division should return inf. Note: currently in numpy, np.divmod(1.0, 0.0) and np.divmod(1, 0) don't return consistent results, its (inf, nan) for former and (0, 0) for latter.

I have currently limited the change to floating point divmod. This will require more changes for int_ divmod to return (inf, nan) (probably has back. compatibility implications) , I am guessing here : https://github.com/numpy/numpy/blob/master/numpy/core/src/umath/loops.c.src#L909

EDIT: inf cannot be returned for int_ divmod , we have to choose between current behavior and raising an error.

@eric-wieser eric-wieser changed the title BUG, WIP: Potential fix for 1//0 to raise divbyzero and return inf BUG, WIP: Potential fix for divmod(1.0, 0.0) to raise divbyzero and return inf May 8, 2020
@eric-wieser
Copy link
Member

Let's limit this PR to the float case, as that has a fairly trivial "correct" behavior (match floor_divide and mod)

@anirudh2290
Copy link
Member Author
anirudh2290 commented May 8, 2020

@eric-wieser I agree, we can limit this to float for now. I sent out an email on the list for a discussion about the same.

@seberg
Copy link
Member
seberg commented May 8, 2020

I somewhat expect we need to check for !b first, and/or change this in divmod. New tests could be nice to make it easier to see what actually changed here. Interesting, but I guess not important p.divmod(4, np.finfo(np.float64).tiny) returns (inf, 0) and sets both overflow and divide by zero flags.

8000
@anirudh2290
Copy link
Member Author

p.divmod(4, np.finfo(np.float64).tiny) returns (inf, 0) and sets both overflow and divide by zero flags.

isn't this expected ? as i raised in the mailing list, I was not sure about whether divmod should set both flags. I thought that since it does both div and mod operations it should set both flags . the ieee754 standard doesnt make a specific comment about this particular operation insists that division or remainder individually should set only one flag.

@anirudh2290
Copy link
Member Author
anirudh2290 commented May 13, 2020

The test failures seem to be because of #16220 and 1.0%0.0 operations not raising invalid warning on mac. I was looking for platform specific tests that omit macOS, but couldnt find any. Is this the first time we are encountering this ?

@anirudh2290
Copy link
Member Author

just so that people don't get misled by this (since I put the comment in the wrong place).

This is likely caused by the clang compiler on mac os likely because clang doesn't implement annex F of C standard, I think this is still open. (https://bugs.llvm.org/show_bug.cgi?id=17005)

the above comment is for the current ci failures on mac with clang. It fails because it doesn't raise the invalid warning on clang builds, since clang doesn't set the invalid flag for 1.0 % 0.0 operations.

@anirudh2290 anirudh2290 force-pushed the ufunc_divide_error branch 7 times, most recently from 482b043 to 4f52add Compare May 19, 2020 18:27
@anirudh2290 anirudh2290 reopened this May 19, 2020
@anirudh2290 anirudh2290 reopened this May 19, 2020
@anirudh2290
Copy link
Member Author

Any idea on where I should be looking to fix this CI error : "ValueError: path is on mount 'D:', start on mount 'C:'". It happened twice in this PR, most likely not related to this PR.

@seberg
Copy link
Member
seberg commented May 19, 2020

No, its failing everywhere today... I thought we fixed something along these lines on master recently, but apparently not, or not robustly.

@anirudh2290
Copy link
Member Author
anirudh2290 commented May 21, 2020

np.fmod(1.0, 0.0) I think will likely fail on Macos. I have added tests for fmod to check. not that its in the divmod code path but maybe good to fix.

Current status:

Operator Warning Message warning after code change Result Result After code change Works as expected on MacOS
np.divmod(1.0, 0.0) Invalid Invalid and Dividebyzero nan, nan inf, nan Yes
np.fmod(1.0, 0.0) Invalid Invalid nan nan No? Yes
np.floor_divide(1.0, 0.0) Invalid dividebyzero nan inf Yes
np.remainder(1.0, 0.0) Invalid Invalid nan nan Yes

EDIT: np.fmod now works on mac os.

@rgommers
Copy link
Member
rgommers commented Aug 7, 2020

This looks about ready, is it good to go from your perspective @anirudh2290?

@anirudh2290
Copy link
Member Author

Thanks for the ping @rgommers ! From my perspective this was ready. Although, the pytorch PR related to raising a domain error for integer np.fmod(1, 0) operations is something this PR doesn't address.

It addresses the changes given in this table:

Operator Warning Message warning after code change Result Result After code change Works as expected on MacOS
np.divmod(1.0, 0.0) Invalid Invalid and Dividebyzero nan, nan inf, nan Yes
np.fmod(1.0, 0.0) Invalid Invalid nan nan No? Yes
np.floor_divide(1.0, 0.0) Invalid dividebyzero nan inf Yes
np.remainder(1.0, 0.0) Invalid Invalid nan nan Yes

It also addresses some behavior changes between different compiler versions for nan or inf usages in these operations. Earlier, this was compiler dependent, now we force set the invalid and divide by zero flags for the operations in the table and it should behave uniformly when compiled with gcc-5 or gcc-8 / gcc-9 for example.

@rgommers rgommers added this to the 1.20.0 release milestone Aug 9, 2020
@charris charris closed this Nov 21, 2020
@charris charris reopened this Nov 21, 2020
@charris charris added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Nov 21, 2020
@charris
Copy link
Member
charris commented Nov 21, 2020

This could use a release note under compatibility since it changes behavior.

@seberg
Copy link
Member
seberg commented Nov 21, 2020

Yeah, this should really be good. There were some smaller reorganization tries I was curious about, but never went down the road, I will try to put that on my TODO list to finish this off.

@charris
Copy link
Member
charris commented Nov 23, 2020

@anirudh2290 Could you add a release note? Look in doc/release/upcoming_changes to see how it is done.

@charris
Copy link
Member
charris commented Nov 24, 2020

I like to make the table part of the release notes. Pandoc translates it to rst without problems but I'd like to check the result first. Anyone know a good linux viewer for rst?

@mattip
Copy link
Member
mattip commented Nov 25, 2020

@charris: try pasting the text below into https://livesphinx.herokuapp.com/

From my perspective this was ready. 
Although, the pytorch PR related to raising a domain error for integer np.fmod(1, 0) 
operations is something this PR doesn't address. 

It addresses the changes given in this table: 

.. list-table:: Some effective title
   :widths: 10 10 25 20 25 10
   :header-rows: 1

   * - Operator
     - Warning Message
     - Result
     - warning after code change
     - Result After code change
     - Works as expected on MacOS
   * - np.divmod(1.0, 0.0)
     - Invalid 
     - Invalid and Dividebyzero
     - nan, nan
     - inf, nan
     - Yes
   * - np.fmod(1.0, 0.0)
     - Invalid 
     - Invalid
     - nan
     - nan
     - No? Yes
   * - np.floor_divide(1.0, 0.0)
     - Invalid 
     - dividebyzero
     - nan
     - inf
     - Yes
   * - np.remainder(1.0, 0.0)
     - Invalid 
     - Invalid
     - nan
     - nan
     - Yes


It also addresses some behavior changes between different compiler versions for nan
or inf usages in these operations. Earlier, this was compiler dependent, now we force
set the invalid and divide by zero flags for the operations in the table and it should behave
 uniformly when compiled with gcc-5 or gcc-8 / gcc-9 for example.

@charris
Copy link
Member
charris commented Nov 25, 2020

Putting this in, I'll make another PR for the release note.

@charris charris merged commit 24a4704 into numpy:master Nov 25, 2020
charris added a commit to charris/numpy that referenced this pull request Nov 25, 2020
charris added a commit that referenced this pull request Nov 25, 2020
@anirudh2290
Copy link
Member Author

Apologies for missing the notification here. Thank you for merging this @charris :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes component: numpy._core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: __floordiv__ gives NaN and invalid warning for 1//0
6 participants
0