Restore integer truncation semantics from BPSER and BUP in the beta_inc translation - #542
Merged
Merged
Conversation
…routines The NSWC Fortran routines rely on implicit typing, so variables starting with I-N are INTEGER and assignments to them truncate. Two spots in the beta_inc translation missed this: - BPSER: M = B0 - 1.0 truncates (only N is declared REAL), and the following DO loop is an integer loop. The translation kept m as Float64 and iterated for i = 1:m over a Float64 range, constructing a TwicePrecision-backed StepRangeLen on every call. The iteration count happens to agree, so this is a pure performance fix (~40ns per call on the 1 < max(a,b) < 8 branch). - BUP: MU = ABS(EXPARG(1)) and K = EXPARG(0) truncate (only L is declared REAL), so the exp scaling exponent is a whole number. No performance impact; restores fidelity to the original. Results are bit-identical on the test suite and on spot checks.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #542 +/- ##
=======================================
Coverage 94.49% 94.49%
=======================================
Files 14 14
Lines 3016 3016
=======================================
Hits 2850 2850
Misses 166 166
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
devmotion
approved these changes
Aug 17, 2026
devmotion
left a comment
Member
There was a problem hiding this comment.
LGTM. I checked the Fortran source and indeed this PR seems to fix inconsistencies with it.
Open
3 tasks
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.
While looking into JuliaStats/StatsFuns.jl#228 (Student-t CDF/quantile performance), I audited the
beta_inctranslation against the original NSWC Fortran source (https://github.com/jacobwilliams/nswc,BRATIOand its subroutines). The Fortran routines rely on implicit typing — variables starting withI–NareINTEGERunless declaredREAL, and assigning a real expression to them truncates. The translation handles this correctly in several places (BRATIO'sN = B0,BRCOMP'sN = B0 - 1.0,BUP'sK = R), but two spots were missed:BPSER(beta_inc_power_series):M = B0 - 1.0truncates (BPSERdeclares onlyNasREAL), andDO 41 I = 1,Mis an integer loop. The translation keptmas aFloat64and iteratedfor i = 1:mover aFloat64range, which constructs aTwicePrecision-backedStepRangeLenon every call — for a loop that typically runs a handful of times. The iteration count happens to coincide with the truncated Fortran semantics, so this is a pure performance fix: ~40 ns flat per call on themin(a,b) < 1 < max(a,b) < 8branch (the cost is the range construction, not the iterations).BUP(beta_inc_diff):MU = ABS(EXPARG(1))andK = EXPARG(0)truncate (BUPdeclares onlyLasREAL), so the scaling exponent is a whole number. This one has no measurable performance impact and produced bit-identical results in all tests; it is included for fidelity to the original.Benchmarks (Apple Silicon,
@belapsed, this branch vs current master; values bit-identical):beta_inc(0.5, 2.5, 0.3103)StatsFuns.tdistcdf(5, 1.5)StatsFuns.tdistinvcdf(5, 0.95)StatsFuns.tdistinvcdf(5, 1e-8)The quantile improves because
beta_inc_invevaluatesbeta_incon every Newton iteration.All
beta_inc/betanctests pass with results unchanged.Disclosure: this PR was prepared by Claude Code at my direction; I have reviewed the changes and the benchmark methodology.
🤖 Generated with Claude Code