-
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
create a sensible comparison trait hierarchy #12520
Conversation
Can we maybe just merge the traits, making |
@pcwalton: This pull request is just attempting to clean up the existing situation rather than changing anything, because there was strong opposition to using the total ordering by default for floating point. |
After this, I plan on renaming This improves the current situation by allowing generic code using the total ordering to have the operators available, while also allowing floating point to continue having deriving of partial ordering available. It's not simple, but it's also not more complex than the current situation so I think it's a clear improvement. |
I agree that this would be an improvement, but why not just merge the two traits ( To be clear:
This solution: (a) provides IEEE 754-compliance; (b) works with hash tables, including using NaNs as keys; (c) supports sorting arrays of floats; (d) has the simplicity of only two traits; (e) works with deriving; (f) "does the right thing" in the vast majority of circumstances, the biggest hazard being people using |
@pcwalton If I'm understanding correctly, then because it would mean generic code couldn't use the comparison operator overloads, because they couldn't be assumed to implement a total order, whereas generic code most often requires one. Enabling generic code (over |
Not all generic code should use a total ordering. Consider sorting: that should definitely not use the total ordering on floats, because the fast total ordering on floats is bitwise less-than, not mathematical less-than. I think code that wants a total ordering should never use the overloaded operators, because the overloaded operators suggest that they do the "mathematical" thing, whereas code that wants a total ordering explicitly doesn't want an ordering which corresponds with the mathematical ordering. |
@pcwalton: The IEEE754 totalOrder does use a sane mathematical ordering. It considers |
It's not fast to implement in software though. I talked to sunfish at lunch and apparently very few people use the IEEE 754 total ordering when they need an ordering for binary trees—they just bitcast to int and use that. |
It does seem like it would have a very significant impact on sort performance. I haven't measured though, and our
|
Floats have multiple orderings useful in different cases, so the only reasonable solution seems to be to force the user to explicitly select which ordering they want for floats. In practice, that means accepting this pull request and adding a bunch of newtypes implementing normal order with NaNs banned, integer order, IEEE 754 total order, normal order except for -nan < -inf, and so on. BTW, I think IEEE 754 total order can be implemented like this, which shouldn't be terribly inefficient (at least if the float is in memory, and thus doesn't need to be moved from FPU registers to ALU registers):
or without the if:
EDIT: fixed, oops, also improved |
is meant to be |
I know this doesn't get us to a final solution, but do we want to merge this for the sake of forward progress? |
I think we should. |
Requires rebase. |
* `Ord` inherits from `Eq` * `TotalOrd` inherits from `TotalEq` * `TotalOrd` inherits from `Ord` * `TotalEq` inherits from `Eq` This is a partial implementation of #12517.
* `Ord` inherits from `Eq` * `TotalOrd` inherits from `TotalEq` * `TotalOrd` inherits from `Ord` * `TotalEq` inherits from `Eq` This is a partial implementation of #12517.
internal: Bring back JodChild into flychecking for cancellation cc https://github.com/rust-lang/rust-analyzer/pull/10517/files#r895241975
Add xFrednet back to the reviewing rotation 🎉 You know what? Having a work-life balance is boring. I truly enjoyed having a free few weeks, and learned that I need to take a break every once in a while, but not doing reviews feels weird. So, this is me coming back for more :D --- r? `@ghost` changelog: none
Ord
inherits fromEq
TotalOrd
inherits fromTotalEq
TotalOrd
inherits fromOrd
TotalEq
inherits fromEq
This is a partial implementation of #12517.