BigInt

A struct representing an arbitrary precision integer.

All arithmetic operations are supported, except unsigned shift right (>>>). Bitwise operations (|, &, ^, ~) are supported, and behave as if BigInt was an infinite length 2's complement number.

BigInt implements value semantics using copy-on-write. This means that assignment is cheap, but operations such as x++ will cause heap allocation. (But note that for most bigint operations, heap allocation is inevitable anyway.)

Constructors

this
this(Range s)

Construct a BigInt from a decimal or hexadecimal string. The number must be in the form of a decimal or hex literal. It may have a leading + or - sign, followed by 0x or 0X if hexadecimal. Underscores are permitted in any location after the 0x and/or the sign of the number.

this
this(bool isNegative, Range magnitude)

Construct a BigInt from a sign and a magnitude.

this
this(T x)

Construct a BigInt from a built-in integral type.

this
this(T x)

Construct a BigInt from another BigInt.

Members

Functions

getDigit
T getDigit(size_t n)

Gets the nth number in the underlying representation that makes up the whole BigInt.

opAssign
BigInt opAssign(T x)

Assignment from built-in integer types.

opAssign
BigInt opAssign(T x)

Assignment from another BigInt.

opBinary
BigInt opBinary(T y)

Implements binary operators between BigInts.

opBinary
BigInt opBinary(T y)

Implements binary operators between BigInt's and built-in integers.

opBinary
auto opBinary(T y)

Implements a narrowing remainder operation with built-in integer types.

opBinaryRight
BigInt opBinaryRight(T y)
T opBinaryRight(T x)

Implements operators with built-in integers on the left-hand side and BigInt on the right-hand side.

opCast
T opCast()

Implements casting to bool.

opCast
T opCast()

Implements casting to integer types.

opCast
T opCast()

Implements casting to floating point types.

opCast
T opCast()

Implements casting to/from qualified BigInt's.

opCmp
int opCmp(BigInt y)
int opCmp(T y)

Implements 3-way comparisons of BigInt with BigInt or BigInt with built-in numeric types.

opEquals
bool opEquals(BigInt y)
bool opEquals(T y)

Implements BigInt equality test with other BigInt's and built-in numeric types.

opOpAssign
BigInt opOpAssign(T y)

Implements assignment operators from built-in integers of the form BigInt op= integer.

opOpAssign
BigInt opOpAssign(T y)

Implements assignment operators of the form BigInt op= BigInt.

opUnary
BigInt opUnary()

Implements BigInt unary operators.

toHash
size_t toHash()
toInt
int toInt()
toLong
long toLong()
toString
void toString(Writer sink, string formatString)
void toString(Writer sink, FormatSpec!char f)
void toString(void delegate(scope const(char)[]) sink, string formatString)
void toString(void delegate(scope const(char)[]) sink, FormatSpec!char f)

Convert the BigInt to string, passing it to the given sink.

Properties

uintLength
size_t uintLength [@property getter]

Number of significant uints which are used in storing this number. The absolute value of this BigInt is always < 232*uintLength

ulongLength
size_t ulongLength [@property getter]

Number of significant ulongs which are used in storing this number. The absolute value of this BigInt is always < 264*ulongLength

Examples

BigInt a = "9588669891916142";
BigInt b = "7452469135154800";
auto c = a * b;
assert(c == BigInt("71459266416693160362545788781600"));
auto d = b * a;
assert(d == BigInt("71459266416693160362545788781600"));
assert(d == c);
d = c * BigInt("794628672112");
assert(d == BigInt("56783581982794522489042432639320434378739200"));
auto e = c + d;
assert(e == BigInt("56783581982865981755459125799682980167520800"));
auto f = d + c;
assert(f == e);
auto g = f - c;
assert(g == d);
g = f - d;
assert(g == c);
e = 12345678;
g = c + e;
auto h = g / b;
auto i = g % b;
assert(h == a);
assert(i == e);
BigInt j = "-0x9A56_57f4_7B83_AB78";
BigInt k = j;
j ^^= 11;
assert(k ^^ 11 == j);

Meta