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
Implement Ord for Complex #106
Comments
It does not make mathematical sense, as discussed in #107, but you should still be able to customize your own sorting.
I'm not sure what you mean here. It's stable as far as the API is concerned, and also stable in the sorting sense where equal items are kept in their original order relative to each other. For types like So if you want to sort slice.sort_unstable_by(|x, y| (x.re, x.im).partial_cmp(&(y.re, y.im)).unwrap()); If you want to permit NaN as well, there's a |
Oh sorry, that's not actually true with |
I managed to get this implemented as described here but there was an interesting wrinkle that made it practically useless for my purposes. I have some code that I'm porting from python where they have a lot of asserts that look like this:
Basically it sorts the results and the expected results then compares that they are all close. Great. I Implement a function which sorted the Complex lexicographically, which is to say that it sorted first by So if you have a list of complex numbers like this: But in practical terms, because of floating point error, values near zero would flip sign, from -ϵ to + ϵ. So in actual use, my results would look like this say: This made the feature useless to me now because the sort order, while still deterministic, was hard to predict unless I just ran the code. So I couldn't rely on it in the end. |
Maybe it would work better for you to sort by polar |
Yeah, I think an optional ϵ parameter to the sort would be helpful. |
I've got to write a lot of unit tests for functions that will be producing lists of complex numbers.
They come out in an arbitrary order so I was hoping to stably sort the expected results and the actual results and then to run a loop over both asserting them to be equal.
I tried doing this with
sort_by()
but it is indeed unstable, and I can't rely on it for these purposes.Would it be possible to implement
Ord
for Complex so that stable sort can be used?The text was updated successfully, but these errors were encountered: