Add RFC float-next-up-down. by orlp · Pull Request #3173 · rust-lang/rfcs · GitH...
source link: https://github.com/rust-lang/rfcs/pull/3173
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Conversation
This RFC adds two argumentless methods to f32
/f64
, next_up
and next_down
. These functions are specified in the IEEE 754 standard, and provide the capability to enumerate floating point values in order.
There's also already an associated implementation pull request (rust-lang/rust#88728), should this RFC pass or be deemed unnecessary.
@kennytm I believe it falls under the 'new API' section, which suggests making an RFC.
Whether new APIs require RFCs generally depends on surface area. Regardless, you've certainly put in the time to justify the change.
I'm in favour of these two functions, for me their usage is rare but I've used them once or twice in other languages. The point of this RFC is to link to it from those function docs, to give complete documentation.
(Regarding the names: as long as the documentation contains the common C function names, I think that next_float and prev_float are simpler to understand for common humans. Rust stdlib generally prefers more self-explanatory function names instead of the common and often more cryptic names. And I agree with this because remembering tens of strange function names isn't nice).
Much like the others, I feel like API addition of this scope does not necessarily warrant a full RFC process. Since we do have a nicely written RFC, perhaps consider making a PR with an implementation in parallel to this RFC as well? I suspect that it would be merged and made available to nightly users significantly before the RFC process concludes.
Two more functions get added to `f32` and `f64`, which may be considered
already cluttered by some.
Additionally, there is a minor pitfall regarding signed zero. Repeatedly calling
This behaviour follows from the definition of the function, doesn't it? The definition says:
Returns the largest/smallest number more/less than
self
.
Since 0.0
and -0.0
compare equal, 0.0.next_down() == -0.0
would violate the contract.
Yes, if we didn't want this we would need to change the definition of the function, and depart from IEEE 754 compliance. But I still felt I needed to mention this potential pitfall.
then `x` is supposed to be returned. Besides error signaling and NaNs, that is
the complete specification.
We did not choose this function for three reasons:
next_up
and next_down
are also easier to implement more efficiently as they don't need to dispatch algorithm based on 2 inputs.
# Unresolved questions
[unresolved-questions]: #unresolved-questions
- Which is the better pair of names, `next_up` and `next_down` or `next_float`
_float
is already implied by the type in Rust.
Much like the others, I feel like API addition of this scope does not necessarily warrant a full RFC process.
Part of the reason I chose to come directly in full force is because next_after
was part of Rust, but got deprecated. I felt I really had to make my case in a thought-out manner.
Since we do have a nicely written RFC, perhaps consider making a PR with an implementation in parallel to this RFC as well?
I could do that, I also have written tests, not part of the RFC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
No one assigned
None yet
No milestone
Successfully merging this pull request may close these issues.
None yet
Recommend
-
4
Copy link Contributor workingjubilee
-
5
6823 members Technology The latest news, reviews and features from the digital and analog world.
-
7
Faster float conversion operations #464 ...
-
6
Collaborator rust-highfive commented
-
5
-
3
Conversation Contributor...
-
3
Glidesort Glidesort is a novel stable sorting algorithm that combines the best-case behavior of Timsort-style merge sorts for pre-sorted data with the best-case behavior of pattern...
-
0
Before your next frontend pull request, use this checklistJune 7, 2023
-
7
Conversation Contributor...
-
4
Conversation Contributor clippy...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK