Skip to content

A compiled gamma ratio past the doubles is taken in logarithms - #2251

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
a-gamma-ratio-past-the-doubles
Oct 30, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
a-gamma-ratio-past-the-doubles

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

A compiled binomial coefficient, falling or rising factorial or Catalan number at an argument that is not whole is a ratio of gamma functions. Past 170! a factorial is past the doubles, though the ratio need not be, so the compiled value was ∞/∞, which is NaN. Where the ratio doesn't come out as a number, it is now taken as the exponential of the difference of the factorials' logarithms. A ratio that does come out as a number is unchanged, to the last place.

master this branch mpmath 1.3.0, 50 digits
binomial(216, pi) NaN 2954596.6790617765 2954596.679061264
fallingfactorial(300, 1/2) NaN 17.3277… 17.32772645443628
catalannumber(2017/5) NaN 5.1595508663051626E+238 5.159550866305278e+238
binomial(-601/2, 7/3) NaN 109419.239… 109419.2391603972
catalannumber(1001) NaN +oo 8.17e597

SpecialFunctions.LogFactorial is the logarithm of Γ(z + 1). It uses the Lanczos approximation the compiled gamma function already uses, with reflection below 1/2. At a real argument its imaginary part is a multiple of π, which gives the ratio's sign.

GammaRatioCompilationTest checks ten values against mpmath under both compilers and over double. All ten fail on master. Evaluation has no value for three of them, and EvalNumerical throws for catalannumber(1001); that is evaluation's, and is left as it is (see #1498).

Suite 18177 passed. The F# wrapper suite, the Mcp, Terminal and InteractiveWrapper suites and the AOT smoke test pass.

Fixes #2248.

🤖 Generated with Claude Code

A compiled binomial coefficient, falling or rising factorial or Catalan
number at an argument that is not whole is a ratio of gamma functions.
Past 170! a factorial is past the doubles where the ratio need not be,
so the ratio was oo/oo, NaN: binomial(216, pi) is about 2954596.68,
fallingfactorial(300, 1/2) about 17.33, catalannumber(2017/5) about
5.16e238, and each was NaN. A ratio whose terms are under the doubles
was 0/0, as binomial(-601/2, 7/3) was, which is about 109419.24.

Where the ratio does not come out as a number, it is now the
exponential of the difference of the factorials' logarithms.
SpecialFunctions.LogFactorial is the logarithm of Gamma(z + 1), by the
Lanczos approximation the compiled gamma function already uses, and by
reflection below 1/2. At a real argument its imaginary part is a
multiple of pi, odd where the factorial is negative, which gives the
ratio's sign. None of the ratios is 0 away from a pole, which the
callers answer first, so a ratio that came out as a number is kept as
it was, to the last place.

GammaRatioCompilationTest compares ten values with mpmath 1.3.0's at 50
digits, under both compilers and over double, and all ten fail on
master. Two of them are past the doubles, binomial(2000, 1001/2) and
catalannumber(1001), and are +oo. Evaluation is NaN for three of the
ten, binomial(10, 601/2), binomial(2000, 1001/2) and
catalannumber(-2017/5), and EvalNumerical throws CannotEvalException
for catalannumber(1001). The last two of the three are past 1e200 or
under 1e-200, the decimal range #1498 is about. Those are evaluation's,
and not changed here.

Found by work/compilesweep.

Fixes #2248.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Rafael-SOWNet Rafael-SOWNet added this to the 2.7.0 milestone Oct 30, 2026
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 30, 2026
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.69%. Comparing base (bd750c4) to head (5bb9b71).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ces/AngouriMath/Numerics/CombinatorialFunctions.cs 88.23% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2251      +/-   ##
==========================================
- Coverage   86.71%   86.69%   -0.02%     
==========================================
  Files         310      310              
  Lines       67785    67800      +15     
  Branches    19664    19671       +7     
==========================================
+ Hits        58781    58782       +1     
- Misses       4892     4903      +11     
- Partials     4112     4115       +3     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.24% <89.47%> (-0.02%) ⬇️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
Sources/AngouriMath/Numerics/SpecialFunctions.cs 80.88% <100.00%> (+0.17%) ⬆️
...ces/AngouriMath/Numerics/CombinatorialFunctions.cs 93.75% <88.23%> (-1.04%) ⬇️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit a17c310 into master Oct 30, 2026
35 checks passed
@Rafael-SOWNet
Rafael-SOWNet deleted the a-gamma-ratio-past-the-doubles branch October 30, 2026 17:52
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

A compiled binomial, factorial power or Catalan number whose factorials are past the doubles is NaN

1 participant