diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp index 45d8454..aa810ab 100644 --- a/include/mcl/vint.hpp +++ b/include/mcl/vint.hpp @@ -1491,9 +1491,10 @@ public: */ static void divMod(VintT *q, VintT& r, const VintT& x, const VintT& y) { - bool qsign = x.isNeg_ ^ y.isNeg_; + bool xNeg = x.isNeg_; + bool qsign = xNeg ^ y.isNeg_; udiv(q, r, x.buf_, x.size(), y.buf_, y.size()); - r.isNeg_ = x.isNeg_; + r.isNeg_ = xNeg; if (q) q->isNeg_ = qsign; } static void div(VintT& q, const VintT& x, const VintT& y) diff --git a/test/vint_test.cpp b/test/vint_test.cpp index d96c1c7..bc60850 100644 --- a/test/vint_test.cpp +++ b/test/vint_test.cpp @@ -973,6 +973,8 @@ CYBOZU_TEST_AUTO(Vint) CYBOZU_TEST_EQUAL(q2, tbl[i].q2); CYBOZU_TEST_EQUAL(r2, tbl[i].r2); CYBOZU_TEST_EQUAL(q2 * b + r2, a); + // alias pattern + // quotRem r2 = 0; Vint::quotRem(&b, r2, a, b); CYBOZU_TEST_EQUAL(b, tbl[i].q2); @@ -1000,6 +1002,36 @@ CYBOZU_TEST_AUTO(Vint) Vint::quotRem(&q, a, a, a); CYBOZU_TEST_EQUAL(q, 1); CYBOZU_TEST_EQUAL(a, 0); + // divMod + a = tbl[i].a; + b = tbl[i].b; + r = 0; + Vint::divMod(&b, r, a, b); + CYBOZU_TEST_EQUAL(b, tbl[i].q); + CYBOZU_TEST_EQUAL(r, tbl[i].r); + b = tbl[i].b; + r = 0; + Vint::divMod(&a, r, a, b); + CYBOZU_TEST_EQUAL(a, tbl[i].q); + CYBOZU_TEST_EQUAL(r, tbl[i].r); + a = tbl[i].a; + q = 0; + Vint::divMod(&q, a, a, b); + CYBOZU_TEST_EQUAL(q, tbl[i].q); + CYBOZU_TEST_EQUAL(a, tbl[i].r); + a = tbl[i].a; + q = 0; + Vint::divMod(&q, a, a, b); + CYBOZU_TEST_EQUAL(q, tbl[i].q); + CYBOZU_TEST_EQUAL(a, tbl[i].r); + a = tbl[i].a; + q = 0; + Vint::divMod(&q, b, a, b); + CYBOZU_TEST_EQUAL(q, tbl[i].q); + CYBOZU_TEST_EQUAL(b, tbl[i].r); + Vint::divMod(&q, a, a, a); + CYBOZU_TEST_EQUAL(q, 1); + CYBOZU_TEST_EQUAL(a, 0); } CYBOZU_TEST_EQUAL(Vint("15") / Vint("3"), Vint("5")); CYBOZU_TEST_EQUAL(Vint("15") / Vint("-3"), Vint("-5"));