src/bigints

Search:
Group by:
Source   Edit  

Arbitrary precision integers.

The bitwise operations behave as if negative numbers were represented in 2's complement.

Types

BigInt = object
An arbitrary precision integer. Source   Edit  

Procs

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  
func `*`(a, b: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
Multiplication for BigInts.

Example:

let
  a = 421.initBigInt
  b = 200.initBigInt
assert a * b == 84200.initBigInt
Source   Edit  
func `+`(a, b: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
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  
func `-`(a, b: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
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  
func `-`(a: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
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  
func abs(a: BigInt): BigInt {....raises: [], tags: [], forbids: [].}

Example:

assert abs(42.initBigInt) == 42.initBigInt
assert abs(-12.initBigInt) == 12.initBigInt
Source   Edit  
func `and`(a, b: BigInt): BigInt {....raises: [Exception], tags: [RootEffect],
                                   forbids: [].}
Bitwise and for BigInts. Source   Edit  
func dec(a: var BigInt; b: int = 1) {....raises: [], tags: [], forbids: [].}
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 inc(a: var BigInt; b: int = 1) {....raises: [], tags: [], forbids: [].}
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 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 `not`(a: BigInt): BigInt {....raises: [Exception], tags: [RootEffect],
                                forbids: [].}
Bitwise not for BigInts. Source   Edit  
func `or`(a, b: BigInt): BigInt {....raises: [Exception], tags: [RootEffect],
                                  forbids: [].}
Bitwise or for BigInts. Source   Edit  
func pow(x: BigInt; y: Natural): BigInt {....raises: [], tags: [], forbids: [].}
Computes x to the power of y. 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  
func `shl`(x: BigInt; y: Natural): BigInt {....raises: [], tags: [], forbids: [].}
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  
func succ(a: BigInt; b: int = 1): BigInt {....raises: [], tags: [], forbids: [].}
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  
func `xor`(a, b: BigInt): BigInt {....raises: [Exception], tags: [RootEffect],
                                   forbids: [].}
Bitwise xor for BigInts. Source   Edit  

Iterators

iterator `..`(a, b: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
Counts from a up to b (inclusive). Source   Edit  
iterator `..<`(a, b: BigInt): BigInt {....raises: [], tags: [], forbids: [].}
Counts from a up to b (exclusive). Source   Edit  
iterator countdown(a, b: BigInt; step: int32 = 1): BigInt {....raises: [],
    tags: [], forbids: [].}
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  

Templates

template `*=`(a: var BigInt; b: BigInt)

Example:

var a = 15.initBigInt
a *= 10.initBigInt
assert a == 150.initBigInt
Source   Edit  
template `+=`(a: var BigInt; b: BigInt)

Example:

var a = 5.initBigInt
a += 2.initBigInt
assert a == 7.initBigInt
Source   Edit  
template `-=`(a: var BigInt; b: BigInt)

Example:

var a = 5.initBigInt
a -= 2.initBigInt
assert a == 3.initBigInt
Source   Edit  
template initBigInt(val: int): BigInt
Source   Edit  
template initBigInt(val: uint): BigInt
Source   Edit