math.big: change the return value of divmod to (quotient, remainder) (#10993)

pull/13461/head
blackshirt 2021-07-30 06:28:32 +07:00 committed by GitHub
parent 6337325676
commit 1eac351f25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -135,9 +135,10 @@ pub fn (n &Number) str() string {
}
mut digits := []byte{}
mut x := n.clone()
div := Number{}
for !x.is_zero() {
mod := divmod(&x, &big.ten, &div)
// changes to reflect new api
div, mod := divmod(&x, &big.ten)
digits << byte(mod.int()) + `0`
x = div
}
@ -192,10 +193,13 @@ pub fn (a &Number) % (b &Number) Number {
return c
}
pub fn divmod(a &Number, b &Number, c &Number) Number {
// divmod returns a pair of quotient and remainder from div modulo operation
// between two bignums `a` and `b`
pub fn divmod(a &Number, b &Number) (Number, Number) {
c := Number{}
d := Number{}
C.bignum_divmod(a, b, c, &d)
return d
C.bignum_divmod(a, b, &c, &d)
return c, d
}
// //////////////////////////////////////////////////////////

View File

@ -90,6 +90,18 @@ fn test_mod() {
assert (big.from_u64(7) % big.from_u64(5)).int() == 2
}
fn test_divmod() {
x, y := big.divmod(big.from_u64(13), big.from_u64(10))
assert x.int() == 1
assert y.int() == 3
p, q := big.divmod(big.from_u64(13), big.from_u64(9))
assert p.int() == 1
assert q.int() == 4
c, d := big.divmod(big.from_u64(7), big.from_u64(5))
assert c.int() == 1
assert d.int() == 2
}
fn test_from_str() {
assert big.from_string('9870123').str() == '9870123'
assert big.from_string('').str() == '0'