added BigInteger parse function
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from error import ParseError
|
||||||
import nwo/clib
|
import nwo/clib
|
||||||
|
|
||||||
type mpz_t = object
|
type mpz_t = object
|
||||||
@@ -15,6 +16,7 @@ cxface:
|
|||||||
proc mpz_set_si(self : mpz_ptr, value : clong) : void
|
proc mpz_set_si(self : mpz_ptr, value : clong) : void
|
||||||
proc mpz_get_str(destination : cstring, base : cint, self : mpz_ptr) : cstring
|
proc mpz_get_str(destination : cstring, base : cint, self : mpz_ptr) : cstring
|
||||||
proc mpz_sizeinbase(self : mpz_ptr, base :cint) : csize_t
|
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(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_add_ui(rop : mpz_ptr, op1 : mpz_ptr, op2 : culong) : void
|
||||||
proc mpz_sub(rop : mpz_ptr, op1 : mpz_ptr, op2 : mpz_ptr) : 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
|
result = $space
|
||||||
dealloc(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 `$`*(n : BigInt) : string = toString(n, 10)
|
||||||
|
|
||||||
proc `+`*(n1 : BigInt, n2 : BigInt) : BigInt =
|
proc `+`*(n1 : BigInt, n2 : BigInt) : BigInt =
|
||||||
|
4
src/mmath/error.nim
Normal file
4
src/mmath/error.nim
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
type
|
||||||
|
SingularMatrixError* = object of ValueError
|
||||||
|
SizeError* = object of ValueError
|
||||||
|
ParseError* = object of ValueError
|
@@ -1,6 +1,7 @@
|
|||||||
from nwo/utils import `-->`
|
from nwo/utils import `-->`
|
||||||
from hvector import HVector, newHVector, buildHVector
|
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
|
from math import sqrt
|
||||||
|
|
||||||
type
|
type
|
||||||
|
@@ -8,8 +8,6 @@ type
|
|||||||
SPivot*[SIZE : static[int], T] = object
|
SPivot*[SIZE : static[int], T] = object
|
||||||
data : array[SIZE, int]
|
data : array[SIZE, int]
|
||||||
permutations* : 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 : HPivot[T], index : int) : int = p.data[index]
|
||||||
proc `[]=`*[T](p : var HPivot[T], index : int, value : int) = p.data[index] = value
|
proc `[]=`*[T](p : var HPivot[T], index : int, value : int) = p.data[index] = value
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
from nwo/utils import `-->`, box
|
from nwo/utils import `-->`, box
|
||||||
from svector import SVector
|
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
|
from math import sqrt
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Reference in New Issue
Block a user