Skip to content
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

Should INT_MIN % -1 return 0 instead of panicking? #1276

Open
glaebhoerl opened this issue Sep 9, 2015 · 4 comments
Open

Should INT_MIN % -1 return 0 instead of panicking? #1276

glaebhoerl opened this issue Sep 9, 2015 · 4 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@glaebhoerl
Copy link
Contributor

As @bill-myers originally pointed out here INT_MIN % -1 is mathematically well-defined to be 0, but Rust currently unconditionally panics instead, as like INT_MIN / -1 it is technically an overflow according to the two's complement representation. If we're incurring a branch to test for this case anyways, then we might as well return the correct result.

("Forked" from #1237.)

@Emerentius
Copy link

Ran into an unexpected panic with the gcd function in the num crate for this reason (euclidean algorithm). The error message telling me that there was an overflow wasn't very helpful either because I just didn't expect it to be from this.

@ticki
Copy link
Contributor

ticki commented Sep 14, 2015

👍 We should generally avoid panics.

@dgrunwald
Copy link
Contributor

👍 0 is obviously the correct result.

C# also handles this the same way: int.MinValue % -1 is 0; int.MinValue / -1 is an overflow error.

@comex
Copy link

comex commented Nov 21, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

6 participants