Skip to content
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

Sort a Vector of Floats - total_cmp vs. partial_cmp #724

Open
Hasnabruzzn opened this issue Feb 3, 2025 · 0 comments
Open

Sort a Vector of Floats - total_cmp vs. partial_cmp #724

Hasnabruzzn opened this issue Feb 3, 2025 · 0 comments

Comments

@Hasnabruzzn
Copy link

In Sort a Vector of Floats partial_cmp is used, which panics with NaN.

For cases where a vector of floats can contain NaNs there is total_cmp (since Rust 1.64?)

    let mut vec = vec![1.1, 1.15, 5.5, 1.123, 2.0, f32::NAN];

    vec.sort_by(|a, b| a.total_cmp(b));

    assert_eq!(vec[0..5], vec![1.1, 1.123, 1.15, 2.0, 5.5]);

I don't know whether partial_cmp would be preferable if there aren't any NaN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant