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
BigInt
from 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
BigInt
from/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
BigInt
and standard integer types besidesint32
are not implemented - api for
BigInt
type is probably unstable (currently it is atuple
, it might become anobject
orref object
in 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 bigints
Flags = enum
Negative
BigInt = tuple[limbs: seq[uint32], flags: set[Flags]]
proc initBigInt(vals: seq[uint32]; flags: set[Flags] = {}): BigInt
proc initBigInt[T: int8 | int16 | int32](val: T): BigInt
proc initBigInt[T: uint8 | uint16 | uint32](val: T): BigInt
proc initBigInt(val: int64): BigInt
proc initBigInt(val: uint64): BigInt
template initBigInt(val: int): BigInt
template initBigInt(val: uint): BigInt
proc initBigInt(val: BigInt): BigInt
zero = ([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): int64
Returns:
- 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): int64
proc cmp(a: int32; b: BigInt): int64
proc `<`(a, b: BigInt): bool
proc `<`(a: BigInt; b: int32): bool
proc `<`(a: int32; b: BigInt): bool
proc `<=`(a, b: BigInt): bool
proc `<=`(a: BigInt; b: int32): bool
proc `<=`(a: int32; b: BigInt): bool
proc `==`(a, b: BigInt): bool
proc `==`(a: BigInt; b: int32): bool
proc `==`(a: int32; b: BigInt): bool
proc `-`(a: BigInt): BigInt
proc `+`(a: BigInt; b: int32): BigInt
proc `+`(a, b: BigInt): BigInt
template `+=`(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): BigInt
template `-=`(a: var BigInt; b: int32)
proc `-`(a, b: BigInt): BigInt
template `-=`(a: var BigInt; b: BigInt)
template optSub{
x = y - z
}(x, y, z: BigInt)
proc `*`(a: BigInt; b: int32): BigInt
template `*=`(a: var BigInt; b: int32)
proc `*`(a, b: BigInt): BigInt
template `*=`(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): BigInt
template optShr{
x = y shr z
}(x, y: BigInt; z)
proc `shl`(x: BigInt; y: int): BigInt
template optShl{
x = y shl z
}(x, y: BigInt; z)
proc reset(a: var BigInt)
proc `div`(a: BigInt; b: int32): BigInt
proc `div`(a, b: BigInt): BigInt
proc `mod`(a: BigInt; b: int32): BigInt
proc `mod`(a, b: BigInt): BigInt
proc 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): T
proc pow(base: int32 | BigInt; exp: int32 | BigInt): BigInt
proc toString(a: BigInt; base: range[2 .. 36] = 10): string
proc `$`(a: BigInt): string
proc 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.}