Arbitrary precision integers.
The bitwise operations behave as if negative numbers were represented in 2's complement.
Procs
func `div`(a, b: BigInt): BigInt {....raises: [Exception], tags: [RootEffect], forbids: [].}
-
Computes the integer division of two BigInt numbers. Raises a DivByZeroDefect if b is zero.
If you also need the modulo (remainder), use the divmod func.
Example:
let a = 17.initBigInt b = 5.initBigInt assert a div b == 3.initBigInt assert (-a) div b == -4.initBigInt assert a div (-b) == -4.initBigInt assert (-a) div (-b) == 3.initBigInt
Source Edit func divmod(a, b: BigInt): tuple[q, r: BigInt] {....raises: [Exception], tags: [RootEffect], forbids: [].}
-
Computes both the integer division and modulo (remainder) of two BigInt numbers. Raises a DivByZeroDefect if b is zero.
Example:
let a = 17.initBigInt b = 5.initBigInt assert divmod(a, b) == (3.initBigInt, 2.initBigInt)
Source Edit func initBigInt(str: string; base: range[2 .. 36] = 10): BigInt {. ...raises: [ValueError], tags: [], forbids: [].}
-
Create a BigInt from a string. For invalid inputs, a ValueError exception is raised.
Example:
let a = initBigInt("1234") b = initBigInt("1234", base = 8) assert a == 1234.initBigInt assert b == 668.initBigInt
Source Edit func initBigInt(val: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
- Source Edit
func initBigInt(val: int64): BigInt {....raises: [], tags: [], forbids: [].}
- Source Edit
func initBigInt(val: uint64): BigInt {....raises: [], tags: [], forbids: [].}
- Source Edit
func initBigInt(vals: sink seq[uint32]; isNegative = false): BigInt {. ...raises: [], tags: [], forbids: [].}
-
Initializes a BigInt from a sequence of uint32 values.
Example:
let a = @[10'u32, 2'u32].initBigInt let b = 10 + 2 shl 32 assert $a == $b
Source Edit func initBigInt[T: int8 | int16 | int32](val: T): BigInt
- Source Edit
func initBigInt[T: uint8 | uint16 | uint32](val: T): BigInt
- Source Edit
func `mod`(a, b: BigInt): BigInt {....raises: [Exception], tags: [RootEffect], forbids: [].}
-
Computes the integer modulo (remainder) of two BigInt numbers. Raises a DivByZeroDefect if b is zero.
If you also need an integer division, use the divmod func.
Example:
let a = 17.initBigInt b = 5.initBigInt assert a mod b == 2.initBigInt assert (-a) mod b == 3.initBigInt assert a mod (-b) == -3.initBigInt assert (-a) mod (-b) == -2.initBigInt
Source Edit func powmod(base, exponent, modulus: BigInt): BigInt {. ...raises: [ValueError, Exception], tags: [RootEffect], forbids: [].}
-
Compute modular exponentation of base with power exponent modulo modulus. The return value is always in the range [0, modulus-1].
Example:
assert powmod(2.initBigInt, 3.initBigInt, 7.initBigInt) == 1.initBigInt
Source Edit func toInt[T: SomeInteger](x: BigInt): Option[T]
-
Converts a BigInt number to an integer, if possible. If the BigInt doesn't fit in a T, returns none(T); otherwise returns some(x).
Example:
import std/options let a = 44.initBigInt b = 130.initBigInt assert toInt[int8](a) == some(44'i8) assert toInt[int8](b) == none(int8) assert toInt[uint8](b) == some(130'u8) assert toInt[int](b) == some(130)
Source Edit func toString(a: BigInt; base: range[2 .. 36] = 10): string {....raises: [], tags: [], forbids: [].}
-
Produces a string representation of a BigInt in a specified base.
Doesn't produce any prefixes (0x, 0b, etc.).
Example:
let a = 55.initBigInt assert toString(a) == "55" assert toString(a, 2) == "110111" assert toString(a, 16) == "37"
Source Edit
Templates
template initBigInt(val: int): BigInt
- Source Edit
template initBigInt(val: uint): BigInt
- Source Edit