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

Integer division/remainder rounding behavior should be documented #26845

Closed
wthrowe opened this issue Jul 7, 2015 · 3 comments · Fixed by #26981
Closed

Integer division/remainder rounding behavior should be documented #26845

wthrowe opened this issue Jul 7, 2015 · 3 comments · Fixed by #26981

Comments

@wthrowe
Copy link
Contributor

wthrowe commented Jul 7, 2015

Using Google I can find lots of old arguments over how integer division should work in Rust, but I cannot find anything in the reference, the API docs for the integer types, or the API docs for the Div trait that says what was actually decided. (And the documentation seems to imply that /, checked_div, and wrapping_div may not all do the same thing.) This is further confused by the integer type documentation apparently sometimes using / to refer to integer division and sometimes to mathematical division. (I can't come up with out any other reasonable interpretation of floor(self / other) in the wrapping_div documentation.) It would be nice if this were clearly documented somewhere, since this varies between languages and is one of the main confusions with integer arithmetic.

@wthrowe wthrowe changed the title Integer division/remainder rounding behavior sould be documented Integer division/remainder rounding behavior should be documented Jul 7, 2015
@steveklabnik
Copy link
Member

http://doc.rust-lang.org/reference.html#behavior-not-considered-unsafe talks about 'overflow' and mentions wrapping, but you're right that we can surface this better.

@arielb1
Copy link
Contributor

arielb1 commented Jul 7, 2015

@steveklabnik

This is about rounding, not wrapping. We use LLVM's sdiv, which rounds towards zero (and srem has the same sign as the dividend). We should document this (the documentation for wrapping_div currently lies, claiming that it rounds towards negative infinite).

bors added a commit that referenced this issue Jul 13, 2015
This resolves #26845.

I'm not entirely satisfied with the placement of the rounding discussion in the docs for the `Div` and `Rem` traits, but I couldn't come up with anywhere better to put it.  Suggestions are welcome.

I didn't add any discussion of rounding to the `checked_div` (or rem) or `wrapping_div` documentation because those seem to make it pretty clear that they do the same thing as `Div`.
@fschutt
Copy link
Contributor

fschutt commented Aug 12, 2017

This should also leave a warning, because integer / integer is not always desired and happens by accident. Just a thought if anyone is reading this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants