Repository navigation
Preserve extended precision in negative-binomial probabilities - #156
Merged
Merged
Conversation
Validate Fp probabilities from their represented value and form 1-p with integer significand arithmetic. Avoid rounding through double, which could turn a probability just below one into one and discard a positive mass. Build coefficients at the requested Fp width without narrowing success counts to uint or overflowing k+r-1. Use exact ulong coefficients for small counts and symmetric numerator/denominator products for larger counts. Divide their integer significands with guard bits instead of using Fp division, which currently converts through hardware real. Replace linear fp_powi multiplication with exponentiation by squaring so large exponents remain practical. Avoid an unused final square and retain the zero-exponent identity. Document rounded arithmetic and the remaining linear cost in the smaller coefficient count. Add Fp64, Fp128, and Fp256 regressions for cancellation near one, complement rounding, endpoint probabilities, full-width counts, and coefficients that require more precision than hardware floating point. Keep the existing public function names and built-in floating-point implementation unchanged. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Expose the corrected Fp implementation through negativeBinomialPMF, using disjoint template constraints to select it from the probability type. Retain fp_negativeBinomialPMF as a forwarding compatibility wrapper so existing callers remain supported without a deprecation warning. Add a documented example that checks the Fp return type and probability, and verifies that a double argument still selects the built-in overload. Document the compatibility name and preserve safe, pure, nothrow, and nogc attributes on both entry points. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Return x directly when fp_powi receives exponent one. Multiplying by one can underflow in Mir's Fp implementation because it adds raw exponents before normalization, discarding a representable result. Add representation-level regressions for positive and negative values near the minimum exponent across Fp64, Fp128, and Fp256. Check signs, coefficients, and exponents without conversion to hardware floats. Cover negativeBinomialPMF(0, 1, p) at the exponent floor, where the mass must equal p. Retain exponentiation by squaring for other powers; the broader Fp multiplication limitation remains a separate issue. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Import Fp inside the documented unittest. CircleCI extracts documentation examples into standalone modules, where the source module's private imports are unavailable, causing the example to fail compilation. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Replace the precision regression's conditional absolute-error calculation with lower and upper bounds on the computed significand. Preserve the existing 16-unit tolerance and exponent check. Avoid an uncovered test branch whose execution depends on rounding direction. Production arithmetic is unchanged. AI-Assisted-By: Codex (GPT-6 Astra Medium)
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.
Fix precision loss and count handling in the extended-precision negative-binomial PMF, and make it available through
negativeBinomialPMFalongside the built-in floating-point implementation.Previously, forming
1-pthroughdoublecould turn anFpprobability just below one into exactly one, incorrectly producing zero mass. Coefficient construction also narrowed success counts touint, could overflow when combining counts, and failed to compile forFp!256because intermediate arithmetic mixed precision widths.Changes
Fprepresentation.ulongarithmetic for small coefficients; use same-width products and integer significand division for larger coefficients, avoidingFpdivision's conversion through hardwarereal.fp_powiwith exponentiation by squaring. Preserve the exponent-one identity directly to avoid premature underflow near the minimum exponent.Fpoverload ofnegativeBinomialPMF, retainingfp_negativeBinomialPMFas a compatibility wrapper without deprecation.Fp!64,Fp!128, andFp!256.The built-in
float,double, andrealPMF implementation is unchanged. Extended-precision results still involve rounding, and coefficient construction remains linear in the smaller count.Validation
Fpmultiplication limitations.Paired local benchmarks showed comparable or improved LDC performance in the tested cases. The medium DMD case was approximately 6% slower; large-exponent cases improved substantially on both compilers.
The existing exponent-boundary limitation in Mir's general
Fpmultiplication is recorded separately for follow-up.AI-Assisted-By: Codex (GPT-6 Astra Medium)