@@ -45,15 +45,19 @@ export const createBigint = /* #__PURE__ */ factory(name, dependencies, ({ typed
4545 * math.bigint(true) // returns 1n
4646 * math.bigint([true, false, true, true]) // returns [1n, 0n, 1n, 1n]
4747 * math.bigint(3**50) // returns 717897987691852578422784n
48- * // note inexactness above from number precision; actual 3n**50n is
49- * // the bigint 717897987691852588770249n
48+ * // note inexactness above from number precision; actual 3n**50n is
49+ * // the bigint 717897987691852588770249n
50+ *
5051 * math.bigint(3**50, {safe: true}) // throws RangeError
5152 * math.bigint(math.pow(math.bignumber(11), 64)) // returns 4457915684525902395869512133369841539490161434991526715513934826000n
52- * // similarly inaccurate; last three digits should be 241
53+ * // similarly inaccurate; last three digits should be 241
54+ *
5355 * math.bigint(
5456 * math.pow(math.bignumber(11), 64),
5557 * {safe: true}) // throws RangeError
5658 * math.bigint(math.fraction(13, 2)) // returns 7n
59+ * math.bigint(math.complex(2.5, -0.3)) // returns 3n
60+ * math.bigint(math.complex(-17, 1)) // throws RangeError
5761 * math.bigint(6.5, {round: 'throw'}) // throws RangeError
5862 * math.bigint(6.5, {round: 'floor'}) // returns 6n
5963 * math.bigint(-6.5, {round: 'ceil'}) // returns -6n
@@ -72,7 +76,6 @@ export const createBigint = /* #__PURE__ */ factory(name, dependencies, ({ typed
7276 '' : function ( ) {
7377 return 0n
7478 } ,
75-
7679 null : function ( x ) {
7780 return 0n
7881 } ,
@@ -101,6 +104,9 @@ export const createBigint = /* #__PURE__ */ factory(name, dependencies, ({ typed
101104 'number | BigNumber | Fraction' : numericToBigint ,
102105 'number | BigNumber | Fraction, Object' : numericToBigint ,
103106
107+ Complex : complexToBigint ,
108+ 'Complex, Object' : complexToBigint ,
109+
104110 'Array | Matrix' : typed . referToSelf ( self => x => deepMap ( x , self ) )
105111 } )
106112
@@ -156,7 +162,9 @@ export const createBigint = /* #__PURE__ */ factory(name, dependencies, ({ typed
156162 if ( / ^ 0 [ b o x ] / . test ( value ) ) return BigInt ( value )
157163
158164 // Otherwise, have to parse ourselves, because BigInt() doesn't allow
159- // rounding; it throws on all decimals.
165+ // rounding; it throws on all decimals. We also can't use parseFloat
166+ // because it will go through the `number` type with its potential loss
167+ // of accuracy.
160168 const match = value . match ( / ^ ( [ + - ] ) ? ( \d * ) ( [ . , ] \d * ) ? ( [ e E ] [ + - ] ? \d + ) ? $ / )
161169 if ( ! match ) {
162170 throw new SyntaxError ( 'invalid BigInt syntax' )
@@ -196,5 +204,15 @@ export const createBigint = /* #__PURE__ */ factory(name, dependencies, ({ typed
196204 return intVal + sgn
197205 }
198206
207+ function complexToBigint ( z , options = { } ) {
208+ if ( numericToBigint ( z . im , options ) !== 0n ) {
209+ throw new RangeError (
210+ `Complex number with nonzero imaginary part ${ z . im } cannot ` +
211+ 'be converted to bigint.'
212+ )
213+ }
214+ return numericToBigint ( z . re , options )
215+ }
216+
199217 return bigint
200218} )
0 commit comments