-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add traits for primitive numeric types #6071
Conversation
Use argument pattern-matching for test_division_rule and remove visibility specifier for test_signed
Having three traits for primitive ints/uints seemed rather excessive. If users wish to specify between them they can simply combine Int with either the Signed and Unsigned traits. For example: fn foo<T: Int + Signed>() { … }
This is a temporary trait until we have default methods. We don't want to encumber all implementors of Ord by requiring them to implement these functions, but at the same time we want to be able to take advantage of the speed of the specific numeric functions (like the `fmin` and `fmax` intrinsics).
These follow the values defined in the C99 standard
@JensNockert can you review this? It looks well-thought-out to me, but I'm not that invested in this subject. |
@bjz This looks really good, and thanks for the detailed explanation in the pull request. |
The `target_word_size` attribute is always available at compile time, so there is no need for a fallback.
fn max(&self, other: &f32) -> f32 { fmax(*self, *other) } | ||
|
||
#[inline(always)] | ||
fn clamp(&self, mn: &f32, mx: &f32) -> f32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is "clamp" NaN-correct? It looks like it will not return NaN if min/max is NaN. It probably should. Just change
_self > *mx to !(_self <= *mx), and similar for the other case.
Ok, updated. Thanks for the help @Erik-S, most appreciated. |
As part of the numeric trait reform (see issue #4819), I have added the following traits to `core::num` and implemented them for Rust's primitive numeric types: ~~~rust pub trait Bitwise: Not<Self> + BitAnd<Self,Self> + BitOr<Self,Self> + BitXor<Self,Self> + Shl<Self,Self> + Shr<Self,Self> {} pub trait BitCount { fn population_count(&self) -> Self; fn leading_zeros(&self) -> Self; fn trailing_zeros(&self) -> Self; } pub trait Bounded { fn min_value() -> Self; fn max_value() -> Self; } pub trait Primitive: Num + NumCast + Bounded + Neg<Self> + Add<Self,Self> + Sub<Self,Self> + Mul<Self,Self> + Quot<Self,Self> + Rem<Self,Self> { fn bits() -> uint; fn bytes() -> uint; } pub trait Int: Integer + Primitive + Bitwise + BitCount {} pub trait Float: Real + Signed + Primitive { fn NaN() -> Self; fn infinity() -> Self; fn neg_infinity() -> Self; fn neg_zero() -> Self; fn is_NaN(&self) -> bool; fn is_infinite(&self) -> bool; fn is_finite(&self) -> bool; fn mantissa_digits() -> uint; fn digits() -> uint; fn epsilon() -> Self; fn min_exp() -> int; fn max_exp() -> int; fn min_10_exp() -> int; fn max_10_exp() -> int; fn mul_add(&self, a: Self, b: Self) -> Self; fn next_after(&self, other: Self) -> Self; } ~~~ Note: I'm not sure my implementation for `BitCount::trailing_zeros` and `BitCount::leading_zeros` is correct for uints. I also need some assistance creating appropriate unit tests for them. More work needs to be done in implementing specialized primitive floating-point and integer methods, but I'm beginning to reach the limits of my knowledge. Please leave your suggestions/critiques/ideas on #4819 if you have them – I'd very much appreciate hearing them. I have also added an `Orderable` trait: ~~~rust pub trait Orderable: Ord { fn min(&self, other: &Self) -> Self; fn max(&self, other: &Self) -> Self; fn clamp(&self, mn: &Self, mx: &Self) -> Self; } ~~~ This is a temporary trait until we have default methods. We don't want to encumber all implementors of Ord by requiring them to implement these functions, but at the same time we want to be able to take advantage of the speed of the specific numeric functions (like the `fmin` and `fmax` intrinsics).
Rustup r? `@ghost` changelog: none
As part of the numeric trait reform (see issue #4819), I have added the following traits to
core::num
and implemented them for Rust's primitive numeric types:Note: I'm not sure my implementation for
BitCount::trailing_zeros
andBitCount::leading_zeros
is correct for uints. I also need some assistance creating appropriate unit tests for them.More work needs to be done in implementing specialized primitive floating-point and integer methods, but I'm beginning to reach the limits of my knowledge. Please leave your suggestions/critiques/ideas on #4819 if you have them – I'd very much appreciate hearing them.
I have also added an
Orderable
trait:This is a temporary trait until we have default methods. We don't want to encumber all implementors of Ord by requiring them to implement these functions, but at the same time we want to be able to take advantage of the speed of the specific numeric functions (like the
fmin
andfmax
intrinsics).