fix: stop prec::ulps_eq! degenerating into a 1e-9 absolute comparison#410
Open
agene0001 wants to merge 1 commit into
Open
fix: stop prec::ulps_eq! degenerating into a 1e-9 absolute comparison#410agene0001 wants to merge 1 commit into
agene0001 wants to merge 1 commit into
Conversation
`approx`'s `ulps_eq` short-circuits on `abs_diff_eq(epsilon)` before it
consults the ULPs distance. The crate's wrapper paired it with
`DEFAULT_EPS = 1e-9`, which made the ULPs bound unreachable, so every internal
`ulps_eq!(x, y)` was really a 1e-9 absolute comparison.
That matters because the macro is used to recognise *exact* parameter values, so
anything within 1e-9 of them silently took a degenerate branch:
Binomial(p = 1 - 2^-33, n = 100).pmf(99) 0 (true 1.16e-8)
Binomial(p = 1 - 2^-33, n = 100).ln_pmf(99) -inf
Geometric(p = 1 - 2^-33).max() 1 (true u64::MAX)
Geometric(p = 1 - 2^-33).skewness() inf (true 92681.9)
Beta(1 + 2^-33, 1 + 2^-33).pdf(0.0) 1 (true 0)
digamma(-1 + 2^-33) -inf (true -8.59e9)
Adds `DEFAULT_ULPS_EPS = f64::EPSILON` and uses it for `ulps_eq!` and
`assert_ulps_eq!`. The exact values still take the degenerate branches -
`Binomial(1.0, 5).pmf(5) == 1`, `Geometric(1.0).max() == 1`, `digamma` at the
poles is still -inf - all covered by existing tests.
Tests use `1 - 2^-33` rather than `1 - 1e-10` so that `1 - p` is exact and the
references are not limited by the representation of `p`.
Also fixes `Binomial::entropy`, which summed `-p * ln(p)` unguarded and so
returned `NaN` once any mass underflowed to zero (`0 * -inf`) - for `p = 0.5`
that is every `n` past about 1100. `Categorical::entropy` already filtered zero
terms; this brings `Binomial` in line.
This was referenced Jul 26, 2026
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.
approx'sulps_eqshort-circuits onabs_diff_eq(epsilon)before it consults the ULPs distance. The crate'sprec::ulps_eq!wrapper paired it withDEFAULT_EPS = 1e-9, which makes the ULPs bound unreachable — every internalulps_eq!(x, y)was really a 1e-9 absolute comparison.That matters because the macro is used at ~25 call sites to recognise exact parameter values (
p == 1.0,x == x.floor(),shape == 1.0), so anything within 1e-9 of them silently took a degenerate branch:The fix adds
DEFAULT_ULPS_EPS = f64::EPSILONand uses it forulps_eq!/assert_ulps_eq!. The exact values still take the degenerate branches (Binomial(1.0, 5).pmf(5) == 1,Geometric(1.0).max() == 1,digammaat the poles is still −inf) — all covered by existing tests, which pass unchanged.New tests use
1 − 2⁻³³rather than1 − 1e-10so1 − pis exact and the mpmath reference values aren't limited by the representation ofp.Also fixes
Binomial::entropy, which summed−p·ln punguarded and returned NaN once any mass underflowed (0 × −inf) — for p = 0.5 that's every n ≳ 1100.Categorical::entropyalready filters zero terms; this bringsBinomialin line.Note: PR for #342 is stacked on this one — the ignored
test_inverse_cdf_small_pturns out to need both fixes.🤖 Generated with Claude Code