Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: numerics reform #369
Conversation
This comment has been minimized.
This comment has been minimized.
|
Earlier drafts of this work were based on the listed alternative: more aggressive inlining of traits. iirc it was down to Float and Int, with SignedInt and UnsignedInt as potentials. FloatMath surviving is clearly motivated by the RFC, but what about the rest? Was it about deduplication and avoiding the more dubious methods like "abs on uints", or is this just a path of least resistance solution? I wasn't under the impression that we were particularly interested in supporting using a generic "Num". |
This comment has been minimized.
This comment has been minimized.
|
@Gankro Thanks for bringing that up. I've revised the RFC with a new paragraph (see "Overview: the new hierarchy") to explain the motivation. I think flattening |
aturon
referenced this pull request
Oct 8, 2014
Closed
Flatten numeric trait hierarchy and allow for user extensibility. #10387
This comment has been minimized.
This comment has been minimized.
|
@Gankro on further reflection, I've decided that the original plan (without |
This comment has been minimized.
This comment has been minimized.
|
(I still think the signed/unsigned separation is a bit silly, but the majority seems to disagree) |
This comment has been minimized.
This comment has been minimized.
jsanders
commented
Oct 8, 2014
|
@aturon: Do you have any links where I can read about the ideas for replacing FromPrimitive and friends? |
This comment has been minimized.
This comment has been minimized.
|
@jsanders See this proposal and the discussion on this issue about |
This comment has been minimized.
This comment has been minimized.
|
Added these links to the RFC. |
nrc
assigned
aturon
Oct 8, 2014
This comment has been minimized.
This comment has been minimized.
|
Was having just Float, SignedInt, and UnsignedInt (and FloatMath as per discussion) ever evaluated? This is a denormalization that has more duplication, but avoids all the "this operation makes no sense" trouble. It also neatly matches what the primitive families actually are. |
This comment has been minimized.
This comment has been minimized.
|
@Gankro Yes, I thought about that possibility. The updated RFC gives a (somewhat weak) justification for Also, while it's a bit far-fetched to imagine useful code that's generic over both ints and floats, being generic over signedness of ints is not so hard to imagine. (Note that the RFC does make some amount of generic programming over natural groupings of the primitive types an explicit goal.) |
This comment has been minimized.
This comment has been minimized.
|
Ah, I didn't process that only some of these would be part of the prelude. Okay, that makes sense. |
This comment has been minimized.
This comment has been minimized.
klutzy
commented
Oct 9, 2014
|
Are the numeric traits expected to be implemented outside of Currently My main concern is that signed integers have asymmetric range so generic functions over |
This comment has been minimized.
This comment has been minimized.
It's not an explicit design goal, but for custom-sized "primitive" types it would probably make sense.
The goal with I would welcome further input on the topic of what needs to go into the prelude. I worry that needing to import |
This comment has been minimized.
This comment has been minimized.
|
Maybe we should be evaluating a way to mark a trait as |
This comment has been minimized.
This comment has been minimized.
brendanzab
reviewed
Oct 10, 2014
| fn is_infinite(self) -> bool; | ||
| fn is_finite(self) -> bool; | ||
| fn is_normal(self) -> bool; | ||
| fn classify(self) -> FPCategory; |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Thanks a bunch for drawing up best proposal for Rust numerics yet! |
This comment has been minimized.
This comment has been minimized.
|
cc. @sebcrozet @SiegeLord |
This comment has been minimized.
This comment has been minimized.
|
So in the docs should we be specific about what types should implement these traits? For example, I am guessing they would not be implemented by big integers and floats...? |
This comment has been minimized.
This comment has been minimized.
|
I have some code that relies only on field operations (Add + Sub + Mul + Div + Zero + One + Clone), and also a type to represent finite-field arithmetic (GF(2^64)) that Rem and the bitwise operations do not make sense. What should be done if this RFC is to be merged? Since it seems Zero and One are going away? |
This comment has been minimized.
This comment has been minimized.
|
@kennytm Nothing prevents you from writing these traits in your own library, which I believe is the overall intent of this RFC: Make algebraic hierarchies a problem for the community to work out in the cargo ecosystem. |
This comment has been minimized.
This comment has been minimized.
|
I am thinking it might be wise to start a numeric lib with things like the additive and multiplicative identities and such. Then folks have a place to turn once we kill some of these numeric traits. I began num-rs quite some time ago... maybe there is something we can salvage from there? |
alexcrichton
force-pushed the
rust-lang:master
branch
from
b9e2b8c
to
5020131
Oct 29, 2014
alexcrichton
reviewed
Oct 29, 2014
| fn exp2(self) -> Self; | ||
| fn ln(self) -> Self; | ||
| fn log2(self) -> Self; | ||
| fn log10(self) -> Self; |
This comment has been minimized.
This comment has been minimized.
alexcrichton
Oct 29, 2014
Member
Some of these have worried me in the past in the sense that although they are LLVM intrinsics, they're often lowered to function calls at compile-time into libraries like libm. By providing these functions in libcore we're semi-stating that they're available for everyone forever (some of them do have x86 instructions, for example), but we're not necessarily delivering on that statement.
A small wory, just wanted to point out why these are singled out for being in libcore rather than libstd. Anyone using these functions and libcore directly will likely already know how to get them anyway
This comment has been minimized.
This comment has been minimized.
This RFC explicitly removes the majority of the numerics traits from the prelude, with the rationale:
The only thing left is a There's clearly a tradeoff involved, and it might be worth instead exposing (In an ideal world, we could instead provide inherent methods directly on primitive types, but I don't think that's likely to happen soon.) |
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Nov 14, 2014
This was referenced Nov 15, 2014
aturon
added a commit
to aturon/rust
that referenced
this pull request
Nov 16, 2014
aturon
referenced this pull request
Nov 16, 2014
Merged
libs: stabilize most numerics after RFC changes #18999
This comment has been minimized.
This comment has been minimized.
ruuda
commented
Nov 16, 2014
|
I am sorry for the late response. I’m only dealing with this now because the changes broke a project of mine. I understand the rationale behind the separation of |
This comment has been minimized.
This comment has been minimized.
|
@ruud-v-a Thanks for the feedback! We're actually considering making |
aturon
added a commit
to aturon/rust
that referenced
this pull request
Nov 16, 2014
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Nov 17, 2014
aturon
referenced this pull request
Nov 17, 2014
Merged
libcore: add num::Int::pow() and deprecate num::pow(). #19031
aturon
added a commit
to aturon/rust
that referenced
this pull request
Nov 19, 2014
aturon
referenced
this pull request
in aturon/rust
Nov 19, 2014
bors
added a commit
to rust-lang/rust
that referenced
this pull request
Nov 20, 2014
michaelwoerister
added a commit
to michaelwoerister/rust
that referenced
this pull request
Nov 20, 2014
This was referenced Nov 24, 2014
klutzy
referenced this pull request
Jan 13, 2015
Closed
Numeric traits inconsistent with integer type system #21069
frewsxcv
referenced this pull request
Jan 24, 2015
Closed
Pow() syntax is inconsistent between floats and ints #9592
This comment has been minimized.
This comment has been minimized.
andydude
commented
Feb 10, 2015
|
The only code associated with this comment are the traits below: pub trait RotateLeft<RHS> {
type Output;
fn rotate_left(self, rhs: RHS) -> Self::Output;
}
pub trait RotateRight<RHS> {
type Output;
fn rotate_right(self, rhs: RHS) -> Self::Output;
}
pub trait SwapBytes<RHS> {
fn swap_bytes(self) -> Self;
fn from_be(x: Self) -> Self;
fn from_le(x: Self) -> Self;
fn to_be(self) -> Self;
fn to_le(self) -> Self;
}I would like to bring attention to the particular use case of using these subsets of the current A second reason why these method subsets are useful to separate from the
After some experimentation, it seems that A side note (and perhaps outside the scope of this comment) is that In conclusion, there are at least 2 compelling reasons (the |
aturon commentedOct 8, 2014
This RFC is preparation for API stabilization for the
std::nummodule. The goal is to finish the simplification efforts started in @bjz's reversal of the numerics hierarcy.Broadly, the proposal is to collapse the remaining numeric hierarchy in
std::num, and to provide only limited support for generic programming (roughly, only over primitive numeric types that vary based on size). Traits giving detailed numeric hierarchy can and should be provided separately through the Cargo ecosystem.Thus, this RFC proposes to flatten or remove most of the traits currently provided by
std::num, and generally to simplify the module as much as possible in preparation for API stabilization.Rendered