X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Farbint.cpp;h=748330fe38b55be76ded37e585dc2e224a5bdd1b;hp=d61dabc9bff6b38f8f8c7ded438cdf3dad6c8bee;hb=a7da45c62d5a0604785479f5dfeb1c2a65d0f9de;hpb=3ab3475a54c82cb9f5e0b1dbb88035f341b92d49 diff --git a/src/arbint.cpp b/src/arbint.cpp index d61dabc..748330f 100644 --- a/src/arbint.cpp +++ b/src/arbint.cpp @@ -108,16 +108,32 @@ Arbint & Arbint::operator*=(const Arbint & mul) void Arbint::Division(const Arbint & div, Arbint & result, Arbint & remainder) const { - //TODO: Optimise? + if (div == Arbint(1L)) + { + result = *this; + remainder = 0L; + return; + } + if (div == *this) + { + result = 1L; + remainder = 0L; + return; + } + + + result = 0L; remainder = *this; - result = 0L; - while ((remainder -= div) > Arbint(0L)) + Arbint next_rem(remainder); + while ((next_rem -= div) >= Arbint(0L)) { - //Debug("Remainder %c%s", remainder.SignChar(), remainder.DigitStr().c_str()); - //Debug("Result %c%s + 1", result.SignChar(), result.DigitStr().c_str()); - result += 1; + //Debug("%li - %li = %li", digit_t(remainder), digit_t(div), digit_t(next_rem)); + //Debug("Sign is %d", next_rem.m_sign); + remainder = next_rem; + result += 1L; } - remainder += div; + + } Arbint & Arbint::operator+=(const Arbint & add) @@ -217,7 +233,8 @@ bool Arbint::IsZero() const bool Arbint::operator==(const Arbint & equ) const { - if (m_sign != equ.m_sign) return false; + if (m_sign != equ.m_sign) + return false; unsigned min_size = m_digits.size(); const Arbint * larger = &equ; if (m_digits.size() > equ.m_digits.size())