added BigInteger parse function

This commit is contained in:
2023-05-28 20:02:00 +08:00
parent a219460d20
commit 42dbf1103d
5 changed files with 17 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
from error import ParseError
import nwo/clib
type mpz_t = object
@@ -15,6 +16,7 @@ cxface:
proc mpz_set_si(self : mpz_ptr, value : clong) : void
proc mpz_get_str(destination : cstring, base : cint, self : mpz_ptr) : cstring
proc mpz_sizeinbase(self : mpz_ptr, base :cint) : csize_t
proc mpz_set_str(self : mpz_ptr, std: cstring, base : cint) : int
proc mpz_add(rop : mpz_ptr, op1 : mpz_ptr, op2 : mpz_ptr) : void
proc mpz_add_ui(rop : mpz_ptr, op1 : mpz_ptr, op2 : culong) : void
proc mpz_sub(rop : mpz_ptr, op1 : mpz_ptr, op2 : mpz_ptr) : void
@@ -68,6 +70,13 @@ proc toString*(n : BigInt, base : cint) : string =
result = $space
dealloc(space)
proc fromString*(t : type[BigInt], s : string, base : int = 10) : BigInt =
new(result)
let mpz : mpz_ptr = addr(result[])
let rc = mpz_set_str(mpz, s, base.cint)
if rc != 0:
raise newException(ParseError, "Cannot parse integer with base " & $base & " from string " & "'" & s & "'")
proc `$`*(n : BigInt) : string = toString(n, 10)
proc `+`*(n1 : BigInt, n2 : BigInt) : BigInt =

4
src/mmath/error.nim Normal file
View File

@@ -0,0 +1,4 @@
type
SingularMatrixError* = object of ValueError
SizeError* = object of ValueError
ParseError* = object of ValueError

View File

@@ -1,6 +1,7 @@
from nwo/utils import `-->`
from hvector import HVector, newHVector, buildHVector
from pivot import HPivot, newHPivot, `[]`, `[]=`, len, SizeError, SingularMatrixError
from pivot import HPivot, newHPivot, `[]`, `[]=`, len
from error import SizeError, SingularMatrixError
from math import sqrt
type

View File

@@ -8,8 +8,6 @@ type
SPivot*[SIZE : static[int], T] = object
data : array[SIZE, int]
permutations* : int
SingularMatrixError* = object of ValueError
SizeError* = object of ValueError
proc `[]`*[T](p : HPivot[T], index : int) : int = p.data[index]
proc `[]=`*[T](p : var HPivot[T], index : int, value : int) = p.data[index] = value

View File

@@ -1,6 +1,7 @@
from nwo/utils import `-->`, box
from svector import SVector
from pivot import SPivot, newSPivot, `[]`, `[]=`, SingularMatrixError, len
from pivot import SPivot, newSPivot, `[]`, `[]=`, len
from error import SizeError, SingularMatrixError
from math import sqrt
type