This library provides a pure implementation for arbitrary precision integers in Nim.
It can be installed through nimble with:
nimble install bigints
bigints provides a BigInt type and related operations with standard Nim syntax:
- creation of
BigIntfrom all standard integer types (initBigInt) - comparisons (
<,<=,==) - addition, negation and subtraction (
+,-,+=-=) - multiplication (
*,*=) - bit shifts (
shr,shl) - integer division and modulo operation (
div,mod) - exponentiation (
^,pow) - conversion of
BigIntfrom/to strings supporting bases from 2 to 36 (initBigInt,$) - iteration utilities (
inc,dec,countdown,countup,..,..<)
Most of the operations above (all those for which it makes sense) are also available between a BigInt and a int32.
For examples of usage see the examples folder.
- not expected to work on 32 bit
- some common bitwise operations (
and,or,xor,not) are not implemented - operations between
BigIntand standard integer types besidesint32are not implemented - api for
BigInttype is probably unstable (currently it is atuple, it might become anobjectorref objectin the future) - arithmetical operations such as addition, multiplication and division are not optimized for performance (e.g. karatsuba multiplication is not implemented)
The following api documentation is generated with mddoc. To regenerate install mddoc with nimble and run
mddoc .\src\bigints.nim
import bigintsFlags = enum
NegativeBigInt = tuple[limbs: seq[uint32], flags: set[Flags]]proc initBigInt(vals: seq[uint32]; flags: set[Flags] = {}): BigIntproc initBigInt[T: int8 | int16 | int32](val: T): BigIntproc initBigInt[T: uint8 | uint16 | uint32](val: T): BigIntproc initBigInt(val: int64): BigIntproc initBigInt(val: uint64): BigInttemplate initBigInt(val: int): BigInttemplate initBigInt(val: uint): BigIntproc initBigInt(val: BigInt): BigIntzero = ([0'u], {})one = ([1'u], {})Returns:
- a value less than zero, if a < b
- a value greater than zero, if a > b
- zero, if a == b
proc cmp(a, b: BigInt): int64Returns:
- a value less than zero, if a < b
- a value greater than zero, if a > b
- zero, if a == b
proc cmp(a: BigInt; b: int32): int64proc cmp(a: int32; b: BigInt): int64proc `<`(a, b: BigInt): boolproc `<`(a: BigInt; b: int32): boolproc `<`(a: int32; b: BigInt): boolproc `<=`(a, b: BigInt): boolproc `<=`(a: BigInt; b: int32): boolproc `<=`(a: int32; b: BigInt): boolproc `==`(a, b: BigInt): boolproc `==`(a: BigInt; b: int32): boolproc `==`(a: int32; b: BigInt): boolproc `-`(a: BigInt): BigIntproc `+`(a: BigInt; b: int32): BigIntproc `+`(a, b: BigInt): BigInttemplate `+=`(a: var BigInt; b: BigInt)template `+=`(a: var BigInt; b: int32)template optAddInt{
x = y + z
}(x, y: BigInt; z: int32)template optAdd{
x = y + z
}(x, y, z: BigInt)proc `-`(a: BigInt; b: int32): BigInttemplate `-=`(a: var BigInt; b: int32)proc `-`(a, b: BigInt): BigInttemplate `-=`(a: var BigInt; b: BigInt)template optSub{
x = y - z
}(x, y, z: BigInt)proc `*`(a: BigInt; b: int32): BigInttemplate `*=`(a: var BigInt; b: int32)proc `*`(a, b: BigInt): BigInttemplate `*=`(a: var BigInt; b: BigInt)template optMulInt{
x = `*`(y, z)
}(x: BigInt{noalias}; y: BigInt; z: int32)template optMulSameInt{
x = `*`(x, z)
}(x: BigInt; z: int32)template optMul{
x = `*`(y, z)
}(x: BigInt{noalias}; y, z: BigInt)template optMulSame{
x = `*`(x, z)
}(x, z: BigInt)proc `shr`(x: BigInt; y: int): BigInttemplate optShr{
x = y shr z
}(x, y: BigInt; z)proc `shl`(x: BigInt; y: int): BigInttemplate optShl{
x = y shl z
}(x, y: BigInt; z)proc reset(a: var BigInt)proc `div`(a: BigInt; b: int32): BigIntproc `div`(a, b: BigInt): BigIntproc `mod`(a: BigInt; b: int32): BigIntproc `mod`(a, b: BigInt): BigIntproc divmod(a: BigInt; b: int32): tuple[q, r: BigInt]proc divmod(a, b: BigInt): tuple[q, r: BigInt]template optDivMod{
w = y div z
x = y mod z}(w, x, y, z: BigInt)template optDivMod2{
w = x div z
x = x mod z}(w, x, z: BigInt)template optDivMod3{
w = w div z
x = w mod z}(w, x, z: BigInt)template optDivMod4{
w = y mod z
x = y div z}(w, x, y, z: BigInt)template optDivMod5{
w = x mod z
x = x div z}(w, x, z: BigInt)template optDivMod6{
w = w mod z
x = w div z}(w, x, z: BigInt)proc `^`[T](base, exp: T): Tproc pow(base: int32 | BigInt; exp: int32 | BigInt): BigIntproc toString(a: BigInt; base: range[2 .. 36] = 10): stringproc `$`(a: BigInt): stringproc initBigInt(str: string; base: range[2 .. 36] = 10): BigInt {.raises: [ValueError], tags: [].}proc inc(a: var BigInt; b: BigInt)proc inc(a: var BigInt; b: int32 = 1)proc dec(a: var BigInt; b: BigInt)proc dec(a: var BigInt; b: int32 = 1)iterator countdown(a, b: BigInt; step: int32 = 1): BigInt {.inline.}iterator countup(a, b: BigInt; step: int32 = 1): BigInt {.inline.}iterator `..`(a, b: BigInt): BigInt {.inline.}iterator `..<`(a, b: BigInt): BigInt {.inline.}