Source
Edit
Arbitrary precision integers.
The bitwise operations behave as if negative numbers were represented in 2's complement.
func `$`(a: BigInt): string {....raises: [], tags: [], forbids: [].}
-
String representation of a BigInt in base 10.
Source
Edit
proc `'bi`(s: string): BigInt {....raises: [ValueError], tags: [], forbids: [].}
-
Create a BigInt from a literal, using the suffix 'bi.
Example:
let
a = 123'bi
b = 0xFF'bi
c = 0b1011'bi
assert $a == "123"
assert $b == "255"
assert $c == "11"
Source
Edit
-
Multiplication for BigInts.
Example:
let
a = 421.initBigInt
b = 200.initBigInt
assert a * b == 84200.initBigInt
Source
Edit
-
Addition for BigInts.
Example:
let
a = 5.initBigInt
b = 10.initBigInt
assert a + b == 15.initBigInt
assert (-a) + b == 5.initBigInt
assert a + (-b) == -5.initBigInt
Source
Edit
-
Subtraction for BigInts.
Example:
let
a = 15.initBigInt
b = 10.initBigInt
assert a - b == 5.initBigInt
assert (-a) - b == -25.initBigInt
assert a - (-b) == 25.initBigInt
Source
Edit
-
Unary minus for BigInt.
Example:
let
a = 5.initBigInt
b = -10.initBigInt
assert (-a) == -5.initBigInt
assert (-b) == 10.initBigInt
Source
Edit
func `<`(a, b: BigInt): bool {....raises: [], tags: [], forbids: [].}
-
Example:
let
a = 5.initBigInt
b = 3.initBigInt
c = 2.initBigInt
assert b < a
assert b > c
Source
Edit
func `<=`(a, b: BigInt): bool {....raises: [], tags: [], forbids: [].}
-
Example:
let
a = 5.initBigInt
b = 3.initBigInt
c = 2.initBigInt
assert a <= b + c
assert c <= b
Source
Edit
func `==`(a, b: BigInt): bool {....raises: [], tags: [], forbids: [].}
-
Compares if two BigInt numbers are equal.
Example:
let
a = 5.initBigInt
b = 3.initBigInt
c = 2.initBigInt
assert a == b + c
assert b != c
Source
Edit
-
Example:
assert abs(42.initBigInt) == 42.initBigInt
assert abs(-12.initBigInt) == 12.initBigInt
Source
Edit
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 fastLog2(a: BigInt): int {....raises: [], tags: [], forbids: [].}
-
Computes the logarithm in base 2 of a. If a is negative, returns the logarithm of abs(a). If a is zero, returns -1.
Source
Edit
func gcd(a, b: BigInt): BigInt {....raises: [Exception], tags: [RootEffect],
forbids: [].}
-
Returns the greatest common divisor (GCD) of a and b.
Example:
assert gcd(54.initBigInt, 24.initBigInt) == 6.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(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 invmod(a, modulus: BigInt): BigInt {....raises: [ValueError, Exception],
tags: [RootEffect], forbids: [].}
-
Compute the modular inverse of a modulo modulus. The return value is always in the range [1, modulus-1]
Example:
assert invmod(3.initBigInt, 7.initBigInt) == 5.initBigInt
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 pred(a: BigInt; b: int = 1): BigInt {....raises: [], tags: [], forbids: [].}
-
Returns the b-th predecessor of a BigInt.
Source
Edit
-
Shifts a BigInt to the left.
Example:
let a = 24.initBigInt
assert a shl 1 == 48.initBigInt
assert a shl 2 == 96.initBigInt
Source
Edit
func `shr`(x: BigInt; y: Natural): BigInt {....raises: [Exception],
tags: [RootEffect], forbids: [].}
-
Shifts a BigInt to the right (arithmetically).
Example:
let a = 24.initBigInt
assert a shr 1 == 12.initBigInt
assert a shr 2 == 6.initBigInt
Source
Edit
-
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
-
Counts from a down to b (inclusive) with the given step count.
Source
Edit
iterator countup(a, b: BigInt; step: int32 = 1): BigInt {....raises: [], tags: [],
forbids: [].}
-
Counts from a up to b (inclusive) with the given step count.
Source
Edit
-
Example:
var a = 15.initBigInt
a *= 10.initBigInt
assert a == 150.initBigInt
Source
Edit
-
Example:
var a = 5.initBigInt
a += 2.initBigInt
assert a == 7.initBigInt
Source
Edit
-
Example:
var a = 5.initBigInt
a -= 2.initBigInt
assert a == 3.initBigInt
Source
Edit