Skip to content

Combined div_rem function (and variants) for ints and uints #774

@Apersoma

Description

@Apersoma

Proposal

Adding a combined division remainder function to integral types.

Problem statement

There is currently no function to do this and computing them together can be done more efficiently than doing them separately.

Motivating examples or use cases

Splitting things into n groups and figuring out how many groups need to have an extra member.
Finding the position of the nth element of a 2d array.
Formatting numbers in different bases.

Solution sketch

// ints & uints
fn div_rem(self, rhs: Self) -> (Self, Self) {
    let div = self / rhs;
    let rem = self - div*rhs;
    (div, rem)
}
// ints & uints
fn checked_div_rem(self, rhs: Self) -> Option<(Self, Self)>;
// ints & uints
unsafe fn unchecked_div_rem(self, rhs: Self) -> (Self, Self);
// ints
fn wrapping_div_rem(self, rhs: Self) -> (Self, Self);
// ints
fn overflowing_div_rem(self, rhs: Self) -> (Self, Self, bool);
// ints
fn strict_div_rem(self, rhs: Self) -> (Self, Self);

Note that saturating is intentionally left out. This is because what the remainder of -256/-1 when the quotient is 255 is either 1 and it is not less than |divisor| or it is 0 and not dividend - quotient*divisor. Wrapping division doesn't run into the issue because the true remainder is 0, and dividend - quotient*divisor is 0 if you let it wrap, which is appropriate for wrapping division.

Unresolved Questions

Should there be variations for div_euclid and div_floor? div_ceil wouldn't be very useful but what about it?
Should Wrapping<N> have div_rem? Saturating<T> shouldn't for the previously mentioned reasons.

Alternatives

Manual implementations of this. Probably some library that has this.

Links and related work

#526
#775
rust-lang/rfcs#914

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions