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

Implement Signed, Unsigned and Natural traits #6041

Closed
wants to merge 3 commits into from

Conversation

brendanzab
Copy link
Member

As part of the numeric trait reform (see issue #4819), I have added the following traits to core::num and implemented them for the appropriate types:

pub trait Signed: Num
                + Neg<Self> {
    fn abs(&self) -> Self;
    fn signum(&self) -> Self;
    fn is_positive(&self) -> bool;
    fn is_negative(&self) -> bool;
}

pub trait Unsigned: Num {}

pub trait Natural: Num
                 + Ord
                 + Quot<Self,Self>
                 + Rem<Self,Self> {
    fn div(&self, other: Self) -> Self;
    fn modulo(&self, other: Self) -> Self;
    fn div_mod(&self, other: Self) -> (Self,Self);
    fn quot_rem(&self, other: Self) -> (Self,Self);

    fn gcd(&self, other: Self) -> Self;
    fn lcm(&self, other: Self) -> Self;
    fn divisible_by(&self, other: Self) -> bool;
    fn is_even(&self) -> bool;
    fn is_odd(&self) -> bool;
}

I have not implemented Natural for BigInt and BigUInt because they're a little over my head. Help with this would be most appreciated.

This adds the following methods to ints and uints:

- div
- modulo
- div_mod
- quot_rem
- gcd
- lcm
- divisible_by
- is_even
- is_odd

I have not implemented Natural for BigInt and BigUInt because they're a little over my head.
@brendanzab
Copy link
Member Author

@gifnksm I didn't want to mess up BigInt and BigUInt by attempting to implement Natural on them. If you have the time, would you be able to have a look into it once these changes are merged?

@brson
Copy link
Contributor

brson commented Apr 24, 2013

This looks excellent. Thanks!

bors added a commit that referenced this pull request Apr 24, 2013
As part of the numeric trait reform (see issue #4819), I have added the following traits to `core::num` and implemented them for the appropriate types:

~~~rust
pub trait Signed: Num
                + Neg<Self> {
    fn abs(&self) -> Self;
    fn signum(&self) -> Self;
    fn is_positive(&self) -> bool;
    fn is_negative(&self) -> bool;
}

pub trait Unsigned: Num {}

pub trait Natural: Num
                 + Ord
                 + Quot<Self,Self>
                 + Rem<Self,Self> {
    fn div(&self, other: Self) -> Self;
    fn modulo(&self, other: Self) -> Self;
    fn div_mod(&self, other: Self) -> (Self,Self);
    fn quot_rem(&self, other: Self) -> (Self,Self);

    fn gcd(&self, other: Self) -> Self;
    fn lcm(&self, other: Self) -> Self;
    fn divisible_by(&self, other: Self) -> bool;
    fn is_even(&self) -> bool;
    fn is_odd(&self) -> bool;
}
~~~

I have not implemented `Natural` for `BigInt` and `BigUInt` because they're a little over my head. Help with this would be most appreciated.
@bors bors closed this Apr 24, 2013
@huonw
Copy link
Member

huonw commented Apr 24, 2013

Shouldn't the Natural methods take &Self?

@gifnksm
Copy link
Contributor

gifnksm commented Apr 24, 2013

@bjz I'm sorry if my previous comment on #6013 made you nervous. But OK. I'll try to implement Natural on BigInt/BigUint.
I notice the same thing that @huonw pointed out. If Natural methods take Self, Natural methods for BigInt need to copy (or move) the whole BigInt value, which may contain very very large size vector. I think these methods should take &Self.
And another point, natural number means "positive integer" (sometimes non-negative integer) in mathematical sense, so it seems strange to me that Natural methods implemented on signed integer types. I think Integer makes more sense in this case, but is there some reasons for naming it as Natural?

@brendanzab
Copy link
Member Author

@gifnksm Heh, no worries! Yeah perhaps Integer would be better. I'll also change the appropriate methods to take borrowed pointers.

bors added a commit that referenced this pull request Apr 28, 2013
This is a follow-up commit for #6041 (and depending on #6048).
Also adding `#[inline(always)]` for almost every methods in `std::bigint`.
flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 9, 2020
…ebroto

Fix FP in `print_stdout`

Fix rust-lang#6041

This lint shouldn't be emitted in `build.rs` as `println!` and `print!` are used for the build script.

changelog: none
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.

5 participants