BigInt is a class in the dart:core library representing an arbitrarily large signed integer. Unlike the primitive int type, which is platform-dependent (fixed 64-bit on native, IEEE 754 double-precision on the web), BigInt provides platform-independent precision limited only by available memory.
Instantiation
BigInt instances are immutable objects created via static methods or factories.
Parsing Strings
Parsing string representations is the primary method for creating large integers to avoid the precision loss inherent in numeric literals.Converting Numbers
Existingint or double values can be converted to BigInt using BigInt.from.
Behavior: The num passed to BigInt.from is converted to an integer. If the input is a double with a fractional part, the value is truncated (rounded towards zero).
Constants
The class provides static constants to minimize allocation overhead for common values.Arithmetic Operations
BigInt implements standard arithmetic operators via operator overloading, with the exception of the standard division operator (/).
Note: The BigInt class does not define the standard division operator (/). Attempting to use it results in a compilation error. Division must be performed using the truncating integer division operator (~/).
Bitwise Operations
Bitwise operators treat the integer as a binary sequence in two’s complement representation.Comparison
BigInt implements the Comparable interface. Comparison operators (<, >, <=, >=) function identically to standard integers.
Type Conversion
ConvertingBigInt back to primitive types requires explicit method calls.
Conversion to String
ThetoString method returns the decimal representation. toRadixString allows specification of the base (2 through 36).
Conversion to int
ThetoInt() method converts the BigInt to a primitive int. The behavior differs by platform if the value exceeds the platform’s int range:
- Native (VM): The result is the least significant 64 bits of the value. This behaves like standard integer overflow (wrapping).
- Web (JavaScript): The result is converted to a JavaScript Number (double). This allows values larger than , but precision is lost for values exceeding the safe integer range. If the value exceeds
double.maxFinite, the result isdouble.infinityordouble.negativeInfinity.
isValidInt to determine if the conversion can occur without loss of precision or overflow.
Conversion to double
ThetoDouble() method converts the value to a floating-point number. This may result in a loss of precision for very large values due to the limitations of IEEE 754 representation.
Memory and Performance
- Heap Allocation: Unlike
int, which is often stack-allocated or handled as a tagged pointer,BigIntis always an object allocated on the heap. - Immutability: Operations on
BigIntproduce new instances; the original object is never modified. - Complexity: Arithmetic operations are significantly slower than primitive
intoperations due to the overhead of arbitrary-precision algorithms. Complexity generally scales with the number of bits in the operand.
Master Dart with Deep Grasping Methodology!Learn More





