Skip to content

Avoid overflow when interpolating finite opposite-sign quantiles - #301

Closed
RealBhupesh wants to merge 3 commits into
d3:mainfrom
RealBhupesh:fix-finite-quantile-overflow
Closed

RealBhupesh wants to merge 3 commits into
d3:mainfrom
RealBhupesh:fix-finite-quantile-overflow

Conversation

@RealBhupesh

Copy link
Copy Markdown

Fixes #300

quantile, quantileSorted, and median can return a non-finite result when interpolating between finite, large opposite-sign values:

quantile([-1e308, 1e308], 0.5);       // Infinity, expected 0
quantileSorted([-1e308, 1e308], 0.5); // Infinity, expected 0
median([-1e308, 1e308]);             // Infinity, expected 0

The intermediate subtraction value1 - value0 overflows. It can also yield NaN at an interior integer position through Infinity * 0, for example the median of [-H, -H, H] with H = 2 ** 1023.

This change uses value0 * (1 - t) + value1 * t only when both selected endpoints are finite and their difference overflows. Otherwise it keeps the original interpolation expression. Selection, endpoint handling, coercion, and accessor calls are unchanged; the median wrapper and index APIs are not changed. This is a narrow intermediate-overflow fix, not a claim of correctly rounded interpolation in general.

The added tests cover symmetric and asymmetric opposite-sign pairs at exact dyadic weights, plus an interior integer position.

Validation on Node v22.23.4, using an isolated plain-Node adaptation:

  • All 20 finite regression calls match their independent expected values with the candidate. The baseline misses all 20. Reverting only the quantile guard loses 12 cases, including median; reverting only the quantileSorted guard loses 8.
  • Baseline and candidate agree under Object.is for 91 finite and 42 non-finite preservation cases each, including signed zero, same-sign extremes, and NaN.
  • The existing assertions from quantile-test.js and median-test.js pass for both versions, including R-7, coercion, accessors, and OneTimeNumber checks. The candidate's added assertions pass too.
  • The adapted harness reports 278 passing checks. It replaces Vitest registration with node:test and the public barrel imports with direct source imports; expected baseline and mutant mismatches are explicitly asserted. This is not a Vitest, full-suite, type-check, build, or CI result. Those checks have not been run locally.

I used AI assistance to prepare this change.

@Fil Fil closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Quantiles overflow for finite opposite-sign inputs

2 participants