static public Bignum Quotient(Bignum numerator, Bignum denominator) { if (denominator.IsZero) { throw new NotImplementedException(); } else if (numerator.IsZero) { return(numerator); } else { bool q_negative_p = denominator.IsNegative ? (!numerator.IsNegative) : numerator.IsNegative; switch (bignum_compare_unsigned(numerator, denominator)) { case bignum_comparison.equal: return((Bignum)(q_negative_p ? -1 : 1)); case bignum_comparison.less: return((Bignum)(0)); case bignum_comparison.greater: { Bignum quotient; Bignum remainder; if (denominator.Length == 1) { BignumDigit digit = denominator [0]; if (digit == 1) { return(numerator.MaybeNewSign(q_negative_p)); } else if (digit < BIGNUM_RADIX_ROOT) { bignum_divide_unsigned_small_denominator(numerator, digit, out quotient, out remainder, q_negative_p, false); } else { bignum_divide_unsigned_medium_denominator(numerator, digit, out quotient, out remainder, q_negative_p, false); } } else { bignum_divide_unsigned_large_denominator(numerator, denominator, out quotient, out remainder, q_negative_p, false); } return(quotient); } default: throw new NotImplementedException(); } } }
static public Bignum Multiply(Bignum left, Bignum right) { int x_length = left.Length; int y_length = right.Length; bool negative_p = left.IsNegative ? (!right.IsNegative) : right.IsNegative; if (left.IsZero) { return(left); } if (right.IsZero) { return(right); } if (x_length == 1) { long digit = left [0].ToLong(); if (digit == 1) { return(right.MaybeNewSign(negative_p)); } if (digit < BIGNUM_RADIX_ROOT) { return(MultiplyUnsignedSmallFactor(right, digit, negative_p)); } } if (y_length == 1) { long digit = right [0].ToLong(); if (digit == 1) { return(left.MaybeNewSign(negative_p)); } if (digit < BIGNUM_RADIX_ROOT) { return(MultiplyUnsignedSmallFactor(left, digit, negative_p)); } } return(MultiplyUnsigned(left, right, negative_p)); }
static public Bignum Quotient (Bignum numerator, Bignum denominator) { if (denominator.IsZero) throw new NotImplementedException (); else if (numerator.IsZero) return numerator; else { bool q_negative_p = denominator.IsNegative ? (!numerator.IsNegative) : numerator.IsNegative; switch (bignum_compare_unsigned (numerator, denominator)) { case bignum_comparison.equal: return (Bignum) (q_negative_p ? -1 : 1); case bignum_comparison.less: return (Bignum) (0); case bignum_comparison.greater: { Bignum quotient; Bignum remainder; if (denominator.Length == 1) { BignumDigit digit = denominator [0]; if (digit == 1) return numerator.MaybeNewSign (q_negative_p); else if (digit < BIGNUM_RADIX_ROOT) bignum_divide_unsigned_small_denominator (numerator, digit, out quotient, out remainder, q_negative_p, false); else bignum_divide_unsigned_medium_denominator (numerator, digit, out quotient, out remainder, q_negative_p, false); } else bignum_divide_unsigned_large_denominator (numerator, denominator, out quotient, out remainder, q_negative_p, false); return quotient; } default: throw new NotImplementedException (); } } }
static public Bignum Multiply (Bignum left, Bignum right) { int x_length = left.Length; int y_length = right.Length; bool negative_p = left.IsNegative ? (!right.IsNegative) : right.IsNegative; if (left.IsZero) return left; if (right.IsZero) return right; if (x_length == 1) { long digit = left [0].ToLong (); if (digit == 1) return right.MaybeNewSign (negative_p); if (digit < BIGNUM_RADIX_ROOT) return MultiplyUnsignedSmallFactor (right, digit, negative_p); } if (y_length == 1) { long digit = right [0].ToLong (); if (digit == 1) return left.MaybeNewSign (negative_p); if (digit < BIGNUM_RADIX_ROOT) return MultiplyUnsignedSmallFactor (left, digit, negative_p); } return MultiplyUnsigned (left, right, negative_p); }