diff --git a/autotests/alkvaluetest.h b/autotests/alkvaluetest.h --- a/autotests/alkvaluetest.h +++ b/autotests/alkvaluetest.h @@ -47,6 +47,7 @@ void subtraction(); void multiplication(); void division(); + void modulo(); void unaryMinus(); void abs(); void precision(); diff --git a/autotests/alkvaluetest.cpp b/autotests/alkvaluetest.cpp --- a/autotests/alkvaluetest.cpp +++ b/autotests/alkvaluetest.cpp @@ -404,6 +404,17 @@ QCOMPARE(m0, AlkValue(-5)); } +void AlkValueTest::modulo() +{ + AlkValue m0(1025000), m1; + m1 = m0 % 97; + QCOMPARE(m1.abs(), AlkValue(1)); + + m0 = 1024999; + m1 = m0 % 97; + QCOMPARE(m1.abs(), AlkValue(0)); +} + void AlkValueTest::unaryMinus() { // AlkValue operator-() const; diff --git a/src/alkvalue.h.in b/src/alkvalue.h.in --- a/src/alkvalue.h.in +++ b/src/alkvalue.h.in @@ -197,6 +197,7 @@ AlkValue operator-(const AlkValue &minuend) const; AlkValue operator*(const AlkValue &factor) const; AlkValue operator/(const AlkValue &divisor) const; + AlkValue operator%(int operand) const; AlkValue operator*(int factor) const; diff --git a/src/alkvalue.cpp b/src/alkvalue.cpp --- a/src/alkvalue.cpp +++ b/src/alkvalue.cpp @@ -444,6 +444,14 @@ return result; } +AlkValue AlkValue::operator%(int operand) const +{ + mpz_class num(mpq_numref(d->m_val.get_mpq_t())); + AlkValue result; + result.d->m_val = num % operand; + return result; +} + AlkValue AlkValue::operator*(int factor) const { AlkValue result;