Skip to content

The Catalan numbers compile - #2216

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
the-catalan-numbers-compile
Oct 30, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
the-catalan-numbers-compile

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Part of #2195, item 3: catalannumber compiles in FastExpression and in the LINQ compiler. It follows the interpreter's terms (#2198):

  • A whole index up to 1000 is exact. It is computed by C_(k + 1) = C_k 2 (2k + 1)/(k + 2) in integers, where the division leaves no remainder, and rounded once. A product of the ratios in doubles rounds at each step, and gave C_5 = 41.99999999999999.
  • At -1 it is -1/2, and at a whole number below that, 0.
  • At a negative half-integer it has no value.
  • Elsewhere it is 4^n Γ(n + 1/2)/(√π Γ(n + 2)).

catalannumber is new since 2.6.0, so its entry says it compiles and there is no 2.6.0 row.

Checked against mpmath (dps 30), compiled and evaluated alike, at 1,191 points: the real axis from -5 to 5 in steps of 0.05, except the negative whole numbers and half-integers, which the tests take, and 1,000 random complex points. All agree to 1e-9.

Tests:

  • TheCatalanNumbersAreWhatEvaluationSays compares both compilers with evaluation on the combinatorial grid and at -4, 10, 30, 7/3 and -7/3.
  • ACatalanNumberAtAWholeIndexIsExact checks C_5 = 42 and C_30 = 3814986502092304 exactly.
  • The AOT smoke test checks C_5 and C_(-1) in a native build.
  • Full suite 17944/0, interactive trio, AOT smoke 71/0, all frameworks build. The full suite ran before the exact whole-index path, and the chain runs it again.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

catalannumber compiles in both compilers, on the interpreter's terms:
exact at a whole index up to 1000, by C_(k + 1) = C_k 2 (2k + 1)/(k + 2)
in integers and rounded once, -1/2 at -1, 0 at a whole number below it,
no value at a negative half-integer, and 4^n Γ(n + 1/2)/(√π Γ(n + 2))
elsewhere. Both compilers agree with evaluation on the combinatorial
grid, and both, with evaluation, with mpmath at 1,191 points on the
real axis and off it.

Part of #2195

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
@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 96.29630% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.61%. Comparing base (3b0889c) to head (c15d05a).

Files with missing lines Patch % Lines
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 75.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2216   +/-   ##
=======================================
  Coverage   86.61%   86.61%           
=======================================
  Files         305      305           
  Lines       67338    67365   +27     
  Branches    19544    19550    +6     
=======================================
+ Hits        58326    58351   +25     
- Misses       4900     4902    +2     
  Partials     4112     4112           
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.17% <96.29%> (+<0.01%) ⬆️
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 Δ
...ath/Functions/Compilation/IntoFE/FastExpression.cs 96.36% <100.00%> (+0.04%) ⬆️
...ctions/Compilation/IntoLinq/CompilationProtocol.cs 90.90% <100.00%> (+0.02%) ⬆️
...h/Functions/Compilation/IntoLinq/MathAllMethods.cs 71.42% <100.00%> (+0.31%) ⬆️
...ces/AngouriMath/Numerics/CombinatorialFunctions.cs 95.89% <100.00%> (+1.15%) ⬆️
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 86.10% <75.00%> (-0.14%) ⬇️

... 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 8f410b8 into master Oct 30, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 30, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the the-catalan-numbers-compile branch October 30, 2026 01:53
Rafael-SOWNet added a commit that referenced this pull request Oct 30, 2026
The compiled binomial took a whole lower argument as a product of ratios
in doubles, (n - i)/(i + 1), each rounded, so binomial(54, 45) came out
5317936259.999995 where evaluation gives 5317936260. Of the 2485
binomials with 0 <= k <= n < 70, 1173 were not the integer. The factorial
powers and the double factorial, products of whole factors, rounded at
each step once a partial product passed 2^53, and so were off in the last
places from there.

At whole arguments each is now computed exactly and rounded once:
- the binomial by binomial(m, i + 1) = binomial(m, i) (m - i)/(i + 1),
  whose division leaves no remainder, over the shorter of k and m - k,
  and for a negative n by (-1)^k binomial(k - n - 1, k);
- the products in doubles while each partial product is below 2^53,
  where every step is exact, and past it in EInteger, rounded by EFloat
  or, for a negative order's reciprocal, by ERational.

The Catalan numbers were computed exactly, in #2216, and rounded by
(double)BigInteger, which truncates: 246 of the first 1000 were a unit
low in the last place, C_32 = 55534064877048200 coming out
55534064877048190. They are rounded by EFloat now. Nothing else in the
library rounds a BigInteger to a double for a result.

A factor of 0 is checked for before the product, so a partial product
past the doubles' range is known to stay past it, and the loop stops
there.

Checked against evaluation exactly, as doubles, in both compilers: every
two-argument function on a grid of 90 whole first arguments, up to 10^12,
and 58 whole second arguments from -12 to 45; the binomial past 2^53 and
past the doubles, among others at (60, 30), (1100, 550), (3000, 1500) and (-1, 4001);
the double factorial from -301 to 320; and the Catalan numbers from 0 to
1000. The expected value is the exact rational rounded once by ERational,
since ToNumerics goes through a decimal whose exponent has a floor, and
reads 1/299!! as 0. On master 9 of these tests fail.

Part of #2195.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

1 participant