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

Define ceiled division, rounding to nearest multiple #16

Merged
merged 10 commits into from
Mar 28, 2019

Conversation

strake
Copy link
Contributor

@strake strake commented Dec 12, 2018

I often find round_up_to in particular useful; round_down_to is also defined for completeness.

div_ceil is also defined as round_up_to is defined in terms of it.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
/// assert_eq!(23.round_up_to(&8), 24);
/// ~~~
#[inline]
fn round_up_to(&self, other: &Self) -> Self where Self: traits::NumRef {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the functionality, but the name seems lacking. I think it could use "multiple" in the name -- maybe next_multiple? Somewhat like the integers' next_power_of_two, which searches >= self.

How should negative self and/or other behave?
(Don't just tell me what it currently does -- think about what it should do first.)

I think I'd rather just clone() instead of introducing NumRef here. Types like BigInt can override with a non-cloning implementation later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could use "multiple" in the name -- maybe next_multiple?

I now called them next_multiple_of and next_multiple_back_of (for consistency with Iterator vs DoubleEndedIterator).

How should negative self and/or other behave?

I'd think it ought to go the other way:
self.next_multiple_of(other) = self.next_multiple_back_of(other.neg())

The new definition fulfills this criterion. I added some tests too.

I think I'd rather just clone() instead of introducing NumRef here

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care for next_multiple_back_of -- "back" makes more sense for iterators than for numbers. I guess you can thing of it like range iterators, but it still feels clumsy. How about previous_multiple_of or prev_multiple_of?

Please test with negative self values. We should explain in words too, that rounding up/down is reversed with negative other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think previous_multiple_of is too long, and the abbreviation prev could be ambiguous — preview is the clash which readily comes to mind, but i'm not sure how relevant it is here. How about past_multiple_of?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like prev better than past. I don't see any precedent in the std API supporting that, but there's a lot of use of prev_ meaning previous throughout rust's implementation. So I think that's well understood, especially in context with next_.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
/// assert_eq!(23.round_down_to(&8), 16);
/// ~~~
#[inline]
fn round_down_to(&self, other: &Self) -> Self where Self: traits::NumRef {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same design questions as round_up_to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer ☺

src/lib.rs Outdated Show resolved Hide resolved
@strake
Copy link
Contributor Author

strake commented Jan 21, 2019

ping

@strake
Copy link
Contributor Author

strake commented Feb 23, 2019

Done, PTAL

@cuviper
Copy link
Member

cuviper commented Mar 27, 2019

Looks good, I just formatted it.

bors r+

bors bot added a commit that referenced this pull request Mar 27, 2019
16: Define ceiled division, rounding to nearest multiple r=cuviper a=strake

I often find `round_up_to` in particular useful; `round_down_to` is also defined for completeness.

`div_ceil` is also defined as `round_up_to` is defined in terms of it.


Co-authored-by: M Farkas-Dyck <strake888@gmail.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors
Copy link
Contributor

bors bot commented Mar 28, 2019

Build succeeded

@bors bors bot merged commit 31ff090 into rust-num:master Mar 28, 2019
@strake strake deleted the ceil+round branch March 28, 2019 18:40
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 this pull request may close these issues.

2 participants