MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / asBigint

Function asBigint

src/compute-engine/boxed-expression/numerics.ts:78–115  ·  view source on GitHub ↗
(
  x: Complex | BigDecimal | ExpressionInput | undefined
)

Source from the content-addressed store, hash-verified

76 return bn / bd;
77}
78
79export function asBigint(
80 x: Complex | BigDecimal | ExpressionInput | undefined
81): bigint | null {
82 if (x === undefined || x === null) return null;
83
84 if (typeof x === 'bigint') return x;
85 if (typeof x === 'number' && Number.isInteger(x)) return BigInt(x);
86
87 if (isExpression(x)) {
88 if (!isNumber(x)) return null;
89 const num = x.numericValue;
90
91 if (typeof num === 'number') {
92 if (Number.isInteger(num)) return BigInt(num);
93 return null;
94 }
95
96 // Extract the exact integer without a precision-limited round-trip.
97 const exact = exactIntegerValue(num);
98 if (exact !== null) return exact;
99
100 if (num.im !== 0) return null;
101
102 // Not an exact integer: only accept a genuine integer-valued float.
103 if (!Number.isInteger(num.re)) return null;
104
105 return BigInt(num.re);
106 }
107
108 if (x instanceof BigDecimal || typeof x === 'string') return bigint(x);
109
110 if (x instanceof Complex) {
111 if (x.im === 0) return bigint(x.re);
112 return null;
113 }
114
115 return bigintValue(x as MathJsonExpression);
116}
117
118export function asBignum(expr: Expression | undefined): BigDecimal | null {

Callers 6

arithmetic.tsFile · 0.90
classifyDomainFunction · 0.90
boxFunctionFunction · 0.90
isPrimeFunction · 0.90
numberFormFunction · 0.90

Calls 6

isExpressionFunction · 0.90
isNumberFunction · 0.90
bigintFunction · 0.90
bigintValueFunction · 0.90
exactIntegerValueFunction · 0.85
isIntegerMethod · 0.45

Tested by

no test coverage detected