Skip to content

Commit 48d8447

Browse files
committed
fix: cleanup
1 parent bb2969d commit 48d8447

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/compute-engine/numerics/rationals.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function add(lhs: Rational, rhs: Rational): Rational {
9696
if (n <= 9007199254740991 && n >= -9007199254740991 && d <= 9007199254740991)
9797
return [n, d];
9898

99-
if (!Number.isFinite(n) || !Number.isFinite(d)) return [n, d];
99+
if (!Number.isFinite(n) || !Number.isFinite(d)) return [NaN, 1];
100100

101101
return [
102102
BigInt(rhsNum[1]) * BigInt(lhs[0]) + BigInt(rhsNum[0]) * BigInt(lhs[1]),
@@ -115,7 +115,7 @@ export function mul(lhs: Rational, rhs: Rational): Rational {
115115
)
116116
return [n, d];
117117

118-
if (!Number.isFinite(n) || !Number.isFinite(d)) return [n, d];
118+
if (!Number.isFinite(n) || !Number.isFinite(d)) return [NaN, 1];
119119

120120
return [BigInt(lhs[0]) * BigInt(rhs[0]), BigInt(lhs[1]) * BigInt(rhs[1])];
121121
}

test/compute-engine/arithmetic.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,27 @@ describe('SUM', () => {
892892
).toMatchInlineSnapshot(`25`);
893893
});
894894

895+
// Regression test for #287: product of Choose values losing precision
896+
it('should compute product of large Choose values exactly', () => {
897+
// Choose(35,7)*Choose(28,7)*Choose(21,7)*Choose(14,7)*Choose(7,7) = 35!/(7!)^5
898+
const expr1 = ce.box([
899+
'Multiply',
900+
['Binomial', 35, 7],
901+
['Binomial', 28, 7],
902+
['Binomial', 21, 7],
903+
['Binomial', 14, 7],
904+
['Binomial', 7, 7],
905+
]);
906+
const expr2 = ce.box([
907+
'Divide',
908+
['Factorial', 35],
909+
['Power', ['Factorial', 7], 5],
910+
]);
911+
expect(expr1.evaluate().sub(expr2.evaluate()).evaluate().toString()).toBe(
912+
'0'
913+
);
914+
});
915+
895916
// Sum of binomial coefficients
896917
it('should simplify sum of binomial coefficients', () => {
897918
expect(

0 commit comments

Comments
 (0)