public NetBigInteger Remainder(
NetBigInteger n)
{
if (n.m_sign == 0)
throw new ArithmeticException("Division by zero error");
if (m_sign == 0)
return Zero;
// For small values, use fast remainder method
if (n.m_magnitude.Length == 1)
{
int val = n.m_magnitude[0];
if (val > 0)
{
if (val == 1)
return Zero;
int rem = Remainder(val);
return rem == 0
? Zero
: new NetBigInteger(m_sign, new int[] { rem }, false);
}
}
if (CompareNoLeadingZeroes(0, m_magnitude, 0, n.m_magnitude) < 0)
return this;
int[] result;
if (n.QuickPow2Check()) // n is power of two
{
result = LastNBits(n.Abs().BitLength - 1);
}
else
{
result = (int[])m_magnitude.Clone();
result = Remainder(result, n.m_magnitude);
}
return new NetBigInteger(m_sign, result, true);
}