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

arith-oflo: what is semantics of divide int::MIN.wrapped_div(-1) #964

Open
pnkfelix opened this issue Mar 11, 2015 · 2 comments
Open

arith-oflo: what is semantics of divide int::MIN.wrapped_div(-1) #964

pnkfelix opened this issue Mar 11, 2015 · 2 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@pnkfelix
Copy link
Member

I am working through implementing #560

I had thought, from the name "wrapping", that int::MIN.wrapped_div(-1) would be defined as follows:

    /// Returns `floor(a / b) mod 2^N`, where `N` is the width of `T` in bits.
    ///
    /// Note that for signed T:
    ///   `(floor(T::MIN / -1) mod 2^N) == (floor(T::MAX + 1) mod 2^N) == 1`
    pub fn overflowing_div<T>(a: T, b: T) -> T;

but then I saw this comment on the RFC thread:

#560 (comment)

which suggests: "there is no undefined behaviour or values left (if we agree to defining the result of INT_MIN/-1 as INT_MAX)."

My thinking is that if one wants the latter semantic, then maybe we should give it a name other than wrapping_div ... e.g. maybe have both:

    /// Returns `floor(a / b) mod 2^N`, where `N` is the width of `T` in bits.
    ///
    /// Note that for signed T:
    ///   `(floor(T::MIN / -1) mod 2^N) == (floor(T::MAX + 1) mod 2^N) == 1`
    pub fn wrapping_div<T>(a: T, b: T) -> T;

    /// Returns `floor(a / b)` clamped to the range `[-2^N, 2^N-1] mod 2^N`, where `N+1` is the width of `T` in bits.
    ///
    /// Note that for signed T:
    ///   `clamp(floor(T::MIN / -1)) == clamp(floor(T::MAX + 1)) == T::MAX`
    pub fn saturating_div<T>(a: T, b: T) -> T;

keywords: arithmetic overflow division divide wrapping

@pnkfelix
Copy link
Member Author

cc @nikomatsakis

@pnkfelix pnkfelix changed the title arith-oflo: what is semantics of int::MIN.wrapped_div(-1) arith-oflo: what is semantics of divide int::MIN.wrapped_div(-1) Mar 27, 2015
@pnkfelix
Copy link
Member Author

(wait was my logic in the above totally bogus? yikes! namely, (floor(T::MAX + 1) mod 2^N) == 0, not 1!)

But in any case, there are other parts in the reasoning above that are really flawed; in particular, it should probably yield T::MIN, by analogy with how wrapping_mul behaves, right? (That is, the description "use floor(somethingsomething) mod 2^N" has no basis in what wrapping signed arithmetic does...)

pnkfelix added a commit to pnkfelix/rust that referenced this issue Apr 14, 2015
See discussion, albeit one-sided, in:

  rust-lang/rfcs#964
alexcrichton pushed a commit to alexcrichton/rust that referenced this issue Apr 22, 2015
See discussion, albeit one-sided, in:

  rust-lang/rfcs#964
@petrochenkov petrochenkov added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Jan 29, 2018
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

2 participants