From 1eac351f25c7e5c5fe40f97853095b7458ba63de Mon Sep 17 00:00:00 2001 From: blackshirt Date: Fri, 30 Jul 2021 06:28:32 +0700 Subject: [PATCH] math.big: change the return value of divmod to (quotient, remainder) (#10993) --- vlib/math/big/big.v | 14 +++++++++----- vlib/math/big/big_test.v | 12 ++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/vlib/math/big/big.v b/vlib/math/big/big.v index 6e9ac668ed..a93aa9e055 100644 --- a/vlib/math/big/big.v +++ b/vlib/math/big/big.v @@ -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 } // ////////////////////////////////////////////////////////// diff --git a/vlib/math/big/big_test.v b/vlib/math/big/big_test.v index c2b3cc6cc5..0742b81598 100644 --- a/vlib/math/big/big_test.v +++ b/vlib/math/big/big_test.v @@ -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'