-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Idea: NonZero-like float wrappers #2458
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
Comments
I definitely think this would be a good idea! See also: |
Could we wait for a more general system based on |
Isn't that kind of an Ouroboros - we want to use const generics to exclude NaN from floats but we can't use floats in const generics because of NaN? |
@glaebhoerl Such a system would likely need to use integer ranges of the bitwise representation anyway, to properly express what they need to do. |
I'd like I'm not sure that the |
My gut feeling is that we shouldn't necessarily mix up these two things:
I don't have super great rationale for that at the moment, but I can vaguely gesture at the following observations:
It seems the underlying theme to what I'm saying is something like: the ideal "this bit pattern can never occur, if it does it's UB" API might look rather different from the ideal "i want to make sure to rule out this invalid value, if i mess up give me a nice error" API. So I'd recommend that we make progress on the general (const generics, bit pattern range based) niche system @eddyb mentioned for providing layout optimizations. Independently, people can continue1 to experiment with APIs for logically-never-NaN newtypes, and when the aforementioned general niche system stabilizes, some of those library types can choose to use it to provide niches in addition to their logical invariants. 1 There's a lot of work in this area already. A quick search for "ord float" on crates.io gives: ordered-float, float-ord, float, decorum, ord_subset, new-ordered-float, noisy_float, and fin. I've not looked at how all of those work but they all claim to address the problem of sorting a collection of floats in some way. |
Going to close this in favour of an ACP: rust-lang/libs-team#238. |
Aren't there a lot of values in the infinite/nan classes that are identical? could those be exploited instead? |
no, none of the different Infinity/NaN values are identical. code can tell them apart by inspecting their bits. also, things like |
(Small correction: there are exactly two infinities with different values because they have different signs, so, those can be told apart. But NaN has an absurd number of values that have totally different bit patterns, and this is a source of a lot of pain for the folks trying to determine float semantics in Rust.) |
those NaNs can also be distinguished using the methods I mentioned in #2458 (comment). that said, LLVM doesn't guarantee which NaNs it will produce for arithmetic operations and iirc some by-value copies (mostly function returns on x86-32) so may produce any arbitrary NaN. |
Optimization strategies such as NaN-boxing rely on being able to introspect NaN values and treat them differently (though a |
Note that restrictions on valid bit patterns would also be needed to be able to use things like the |
Basically, the types
NonNanF32/64
andFiniteF32/64
. The main reasoning of having these in the standard library is to allow niche optimisations.Additionally, both of these types could implement
Ord
asNaN
wouldn't be valid for either.The text was updated successfully, but these errors were encountered: