-
Notifications
You must be signed in to change notification settings - Fork 20
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
ACP: Sign
enum for indicating sign of a number
#377
Comments
Sign
enum for indicating sign of a number
it will make |
I completely forgot how multiplication worked. Oops. |
We reviewed this in today's @rust-lang/libs-api meeting. There were various opinions on this. Some folks weren't in favor of adding this as a trait. Others were in favor of adding numeric traits in general but not in favor of adding a trait that introduces a new subtly different semantic from the existing The combination of those two things led us to reject this ACP. |
@joshtriplett I'm confused because this ACP doesn't seem to propose a new trait at all |
I'm sorry but did the lib team confuse this ACP with some other proposal? OP suggested adding an enum not a trait. (I'm neutral to whether accepting this ACP but the rejection rationale keep talking about "trait" is just very confusing) |
I was at the meeting but didn't take notes so the following is just my fallible recollection. In the meeting some felt that the existence of signum makes it weird if there's also an enum which has slightly different behaviour. Others felt that this is fairly niche. If you actually need this you're likely already using some numerics crate (or your own lib), which could provide such an enum. |
My apologies, I clearly picked up an incorrect detail from the meeting, and had the impression this was proposing a trait to contain the Even in the absence of that, though, the remainder of the consensus still holds: we don't want to add a method that seems confusingly similar to the |
Proposal
Problem statement
The standard library offers
Ordering
as a simple enum of -1, 0, or 1, but its meaning is limited to ordering specifically, and isn't suitable for many other uses. Additionally, the presence of zero might be undesired in some cases, since for example, floating-point numbers always have a positive or negative sign.Motivating examples or use cases
A lot of APIs could benefit from an enum specifically dedicated to indicating a positive or negative sign, like the currently unstable
ln_gamma
function (rust-lang/rust#99842) and the varioussignum
methods offered by numbers which could return a sign instead.Additionally, many APIs outside the standard library could benefit from a standardised "sign" type as a simple way of ensuring that an input is specifically limited to the values ±1.
Solution sketch
Alternatives
Ordering
could be used, but this is less than ideal because it's only equivalent in layout, not meaning, and also has the same problem of including zero.Additional possible extensions:
Div
could be implemented in addition toMul
forSign
, since division by a sign is equivalent to multiplying. This is left out because it would probably just be confusing.Option<Sign>
could have methods similar to those onSign
whereNone
is treated as zero, but this seems less than optimal since floats useNone
to represent NaN, not zero.Links and related work
N/A
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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: