Repository navigation
Do not panic when sorting a float array that contains NaN - #1216
Merged
Merged
Conversation
Collaborator
|
There is a failed test in CI possibly due to , |
Contributor
Author
|
Fixed. The test script uses method-call syntax (a.push, a.sort), which doesn't parse under no_object, so test_arrays_sort_nan is now gated on not(no_object) like the other method-based tests in tests/arrays.rs. Checked it locally with the testing-environ,no_object,serde,metadata,internals,debugging,grain row. The codegen job failures are a separate thing: stable just moved to rustc 1.100.0 and the trybuild ui_tests stderr no longer matches, and this PR doesn't touch codegen. |
Contributor
Author
|
Thanks for merging, glad the NaN sort panic is gone. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sorting an array of floats that contains NaN panics:
The built-in float comparison in
sort_with_builtinandsort_with_builtin_descispartial_cmp(..).unwrap_or(Ordering::Equal), so NaN is equal to every number while those numbers are not equal to each other. Since Rust 1.81,slice::sort_bypanics when it detects an order like that.sort_bywith a custom comparer is already wrapped incatch_unwindfor this reason, but the built-in path is not, sosortorsort_descon a float array holding a NaN (0.0 / 0.0,sqrt(-1.0),parse_float("NaN")) can take down the host with no engine limit in the way. On random arrays of up to 61 floats, a quarter of them NaN, about two thirds of calls panic for each method.When
partial_cmpreturnsNonethe comparison now falls back tois_nan(), so NaN sorts after every number forsortand before every number forsort_desc. Everything that is not NaN compares exactly as before, so-0.0and0.0still compare equal and keep their order. I did not usetotal_cmpbecause it orders NaN by its sign bit, and the NaN that0.0 / 0.0produces is negative on x86 and positive on ARM, so NaN would land at different ends on different machines.Added
test_arrays_sort_nantotests/arrays.rs. Without the fix it panics on bothsortandsort_desc; with it, it passes.cargo test --testspasses with default features and with thef32_float,unchecked,sync,f32_float,only_i32,no_float,decimalandno_indexrows of the CI matrix.cargo fmt --check, the CI clippy invocation andcargo +1.66.0 checkagainstCargo.msrv.lockare clean. CHANGELOG line under 1.27.1's bug fixes.