Conversation
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
|
Does |
|
n1 + 1 and k + 1 are safe: the swap above guarantees n1 <= n2 so n1 <= n/2, and k = min(sample_size, n - sample_size) <= n/2, so only the n + 2 term could actually overflow. The f64 saturation is inherent to computing m in f64 once n exceeds 2^53 (k as f64 and n1 as f64 lose precision the same way) and predates this change; as far as I can tell an off-by-one mode only shifts the HIN/H2PE selection right at the threshold, but happy to clamp or document it if you'd prefer. |
CHANGELOG.mdentrySummary
Fixes #59.
Hypergeometric::newcomputedn + 2inu64, which overflows oncetotal_population_sizereachesu64::MAX - 1.Motivation
For a population near
u64::MAX(with validK <= Nandn <= N), the constructor should build a usable sampler. Instead, then + 2term overflowed: in debug/overflow-checked buildsnewpanicked, and in release the wrap made the modeminfinite, which selected the H2PE branch with NaN constants sosamplelater panicked withEmptyRange.Details
Compute the
+ 2inf64(n as f64 + 2.0) so the divisor no longer overflowsu64. The mode is then computed correctly (~0for the reproducer), the inverse-transform branch is selected, and sampling returns values in the expected support. Added a regression test constructingHypergeometric::new(u64::MAX - 1, 3, 2)and drawing samples.