Skip to content

fix(data): do not panic on NaN in Quartiles::new - #750

Open
newmathphys wants to merge 1 commit into
plotters-rs:masterfrom
newmathphys:fix/quartiles-nan-panic
Open

newmathphys wants to merge 1 commit into
plotters-rs:masterfrom
newmathphys:fix/quartiles-nan-panic

Conversation

@newmathphys

Copy link
Copy Markdown

Fixes #749.

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 unwrap() aborts the caller's program — from data, inside a plotting call, not from a programmer mistake.

NaN is a legal f64 and turns up in real data (missing measurements, 0.0 / 0.0, null deserialised to float), and the doc comment on new states no NaN precondition.

The change sorts NaN as equal, which keeps the result defined:

s.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

(total_cmp is not usable directly here: T is generic and only PartialOrd is required.)

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

The regression test sits next to the existing ones in the same file.

One thing I did not change: percentile_of_sorted starts with assert!(!s.is_empty()), so Quartiles::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-sort pass — 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.

`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.
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

Successfully merging this pull request may close these issues.

[BUG] Quartiles::new panics on NaN in the input slice

1 participant