Skip to content

Commit 1430582

Browse files
committed
Release v2.1.0: Improve overflow handling and add comprehensive tests
- Replace silent infinity returns with explicit OverflowException - Add overflow detection during calculations - Expand test coverage with edge cases - Update documentation for breaking changes - Bump version to 2.1.0
1 parent 05b43d3 commit 1430582

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

MathFlow.Core/Combinatorics/CombinatoricsFunctions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static double Factorial(double n)
3636
throw new ArgumentException("Factorial is only defined for non-negative integers");
3737

3838
if (n > 170)
39-
return double.PositiveInfinity;
39+
throw new OverflowException($"Factorial of {n} would overflow. Maximum supported value is 170.");
4040

4141
double result = 1;
4242
for (int i = 2; i <= (int)n; i++)
@@ -53,7 +53,7 @@ public static double Binomial(double n, double k)
5353
if (n < 0 || n != Math.Floor(n) || k < 0 || k != Math.Floor(k))
5454
throw new ArgumentException("Binomial is only defined for non-negative integers");
5555

56-
if (n < 0 || k < 0 || k > n)
56+
if (k > n)
5757
return 0;
5858

5959
if (k > n - k)
@@ -64,6 +64,9 @@ public static double Binomial(double n, double k)
6464
{
6565
result *= n - (k - i);
6666
result /= i;
67+
68+
if (double.IsInfinity(result))
69+
throw new OverflowException($"Binomial coefficient C({n},{k}) would overflow.");
6770
}
6871

6972
return result;
@@ -79,8 +82,13 @@ public static double Permutation(double n, double k)
7982

8083
double result = 1;
8184
for (int i = 0; i < k; i++)
85+
{
8286
result *= n - i;
8387

88+
if (double.IsInfinity(result))
89+
throw new OverflowException($"Permutation P({n},{k}) would overflow.");
90+
}
91+
8492
return result;
8593
}
8694
}

MathFlow.Core/MathFlow.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
88
<PackageId>MathFlow</PackageId>
9-
<Version>2.0.0</Version>
9+
<Version>2.1.0</Version>
1010
<Authors>Nonanti</Authors>
1111
<Description>C# math expression library with symbolic computation support. Parse, evaluate, differentiate, simplify mathematical expressions.</Description>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -19,7 +19,7 @@
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
2020
<PackageIcon>icon.png</PackageIcon>
2121
<Copyright>Copyright (c) 2025 Nonanti</Copyright>
22-
<PackageReleaseNotes>v2.0.0: Complex numbers, enhanced polynomial factoring, new ODE solvers, rational function integration, matrix operations</PackageReleaseNotes>
22+
<PackageReleaseNotes>v2.1.0: Added combinatorics functions (binomial, permutation), improved overflow handling, reorganized math functions into CombinatoricsFunctions class</PackageReleaseNotes>
2323
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

MathFlow.Tests/CombinatoricsTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,43 @@ public CombinatoricsTests()
1414
[InlineData("gcd(12, 18)", 6)]
1515
[InlineData("lcm(12, 18)", 36)]
1616
[InlineData("factorial(5)", 120)]
17+
[InlineData("factorial(0)", 1)]
18+
[InlineData("factorial(10)", 3628800)]
1719
[InlineData("perm(5, 3)", 60)]
1820
[InlineData("Permutation(5, 3)", 60)]
1921
[InlineData("perm(4, 2)", 12)]
2022
[InlineData("perm(6, 0)", 1)]
2123
[InlineData("perm(0, 0)", 1)]
2224
[InlineData("binomial(5, 3)", 10)]
2325
[InlineData("binomial(6, 0)", 1)]
26+
[InlineData("binomial(10, 5)", 252)]
2427
[InlineData("ncr(6, 6)", 1)]
2528
[InlineData("choose(6, 2)", 15)]
2629
public void Calculate_CombinatoricsFunctions_ReturnsCorrectResult(string expression, double expected)
2730
{
2831
var result = _engine.Calculate(expression);
2932
Assert.Equal(expected, result, 10);
3033
}
34+
35+
[Theory]
36+
[InlineData("factorial(171)")]
37+
[InlineData("factorial(200)")]
38+
[InlineData("perm(200, 100)")]
39+
[InlineData("binomial(1000, 500)")]
40+
public void Calculate_LargeNumbers_ThrowsOverflowException(string expression)
41+
{
42+
Assert.Throws<OverflowException>(() => _engine.Calculate(expression));
43+
}
44+
45+
[Theory]
46+
[InlineData("factorial(-1)")]
47+
[InlineData("factorial(5.5)")]
48+
[InlineData("perm(5, 6)")]
49+
[InlineData("perm(-1, 2)")]
50+
[InlineData("binomial(5.5, 2)")]
51+
[InlineData("binomial(5, -1)")]
52+
public void Calculate_InvalidInputs_ThrowsArgumentException(string expression)
53+
{
54+
Assert.Throws<ArgumentException>(() => _engine.Calculate(expression));
55+
}
3156
}

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dotnet add package MathFlow
2222

2323
### Package Reference
2424
```xml
25-
<PackageReference Include="MathFlow" Version="2.0.0" />
25+
<PackageReference Include="MathFlow" Version="2.1.0" />
2626
```
2727

2828
### Package Manager Console
@@ -46,11 +46,21 @@ var vars = new Dictionary<string, double> { ["x"] = 3, ["y"] = 4 };
4646
var answer = engine.Calculate("x^2 + y^2", vars); // 25
4747
```
4848

49+
## What's New in v2.1.0
50+
51+
### New Features
52+
- **Combinatorics Functions**: Added binomial coefficients and permutation calculations
53+
- **Reorganized Math Functions**: GCD, LCM, and factorial moved to dedicated CombinatoricsFunctions class
54+
55+
### Breaking Changes
56+
- `GCD`, `LCM`, and `Factorial` functions have been moved from their previous location to `CombinatoricsFunctions` class
57+
- Update your imports if you were using these functions directly
58+
4959
## What's New in v2.0.0
5060

5161
### Major Features Added
5262
- **Complex Number Support**: Full integration with expression parser
53-
- **Enhanced Polynomial Factoring**: Quadratic, cubic, and special forms
63+
- **Enhanced Polynomial Factoring**: Quadratic, cubic, and special forms
5464
- **New ODE Solvers**: RungeKutta2 and Adams-Bashforth methods
5565
- **Rational Function Integration**: Partial fractions and special forms
5666
- **Symbolic Integration**: Extended support for trigonometric and exponential functions

0 commit comments

Comments
 (0)