Repository navigation
fix(data): do not panic on NaN in Quartiles::new - #750
Open
newmathphys wants to merge 1 commit into
Open
newmathphys wants to merge 1 commit into
newmathphys wants to merge 1 commit into
Conversation
`Quartiles::new` sorts the copied slice with
`s.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap())`. For a numeric series
(`T = f64`/`f32`) a single `NaN` makes `partial_cmp` return `None`, and the
`unwrap()` aborts the caller's program.
`NaN` is a legal `f64` and appears in real data (missing measurements,
`0.0 / 0.0`, deserialised `null`), and the doc comment on `new` states no NaN
precondition, so today bad data becomes a process panic instead of a plot.
Sort NaN as equal, which keeps the result defined:
a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
Measured on this tree, `cargo test -p plotters --lib data::quartiles`:
without the fix: test_nan_in_data_does_not_panic ... FAILED
panicked at plotters/src/data/quartiles.rs:51:52 (2 passed; 1 failed)
with the fix: test_nan_in_data_does_not_panic ... ok
3 passed; 0 failed
Regression test added next to the existing ones in the same file.
newmathphys
pushed a commit
to newmathphys/kern
that referenced
this pull request
Dec 1, 2026
…е отстаёт Отправлен PR [plotters#750](plotters-rs/plotters#750): одна строка фикса + регресс-тест, и «до/после» измерено НА САМОМ КРЕЙТЕ, а не на модели поведения: без фикса: test_nan_in_data_does_not_panic ... FAILED panicked at plotters/src/data/quartiles.rs:51:52 (2 passed; 1 failed) с фиксом: test_nan_in_data_does_not_panic ... ok (3 passed; 0 failed) Реестр (Case Y) и строка исходов переведены из «Sent, awaiting reply» в «PR open»; блок-лист ссылается на оба номера. Документ, который говорит «отправлено», когда уже есть PR, — это тот же класс, что #335/#350, поэтому правка в тот же день. ПРОВЕРЕНО: docs-parity, claims-audit, metrics-guard, docs-map-check, hunt-gates, readme-link-check — rc=0.
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.
Fixes #749.
Quartiles::newsorts the copied slice withs.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap()). For a numeric series (T = f64/f32) a singleNaNmakespartial_cmpreturnNone, andunwrap()aborts the caller's program — from data, inside a plotting call, not from a programmer mistake.NaNis a legalf64and turns up in real data (missing measurements,0.0 / 0.0,nulldeserialised to float), and the doc comment onnewstates no NaN precondition.The change sorts NaN as equal, which keeps the result defined:
(
total_cmpis not usable directly here:Tis generic and onlyPartialOrdis required.)Measured on this tree,
cargo test -p plotters --lib data::quartiles:The regression test sits next to the existing ones in the same file.
One thing I did not change:
percentile_of_sortedstarts withassert!(!s.is_empty()), soQuartiles::new(&[])also panics. That is a different precondition (empty input, not bad data) — say the word and I will fold it in or send it separately.Disclosure: found with KERN, a verifier I maintain (static
nan-sortpass —partial_cmp(..).unwrap()over floats in a public API), then read by hand; the patch and this text were drafted with AI assistance and reviewed line by line by me.