Exemplo n.º 1
0
 /// <summary>
 /// The method add two field togther.
 /// - Only Exact rationals are supported.
 /// </summary>
 /// <param name="f"></param>
 /// <returns></returns>
 public override OrderedField Add(OrderedField f)
 {
     if (f is ExactRational)
     {
         ExactRational that = f as ExactRational;
         BigInteger    thisn = n, thisd = d, thatn = that.n, thatd = that.d;
         var           commonnumerator = thisn * thatn;
         thisn *= thatd;
         thatn *= thisd;
         return(ConstructExactRational(thisn + thatn, commonnumerator));
     }
     throw new NotImplementedException();
 }
Exemplo n.º 2
0
 public override int CompareTo(OrderedField other)
 {
     if (other is ExactRational)
     {
         ExactRational that       = other as ExactRational;
         var           difference = this - that;
         if (difference.IsZero())
         {
             return(0);
         }
         return(difference.IsPositive() ? 1 : -1);
     }
     throw new NotImplementedException();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Public static method for getting an instance of the class.
        /// </summary>
        /// <Exception>
        /// Divided by zero.
        /// </Exception>
        /// <param name="numerator"></param>
        /// <param name="denominator"></param>
        /// <returns>
        ///
        /// </returns>
        public static ExactRational ConstructExactRational
            (BigInteger numerator, BigInteger denominator)
        {
            if (denominator.IsZero)
            {
                throw new DivideByZeroException();
            }
            var res          = new ExactRational();
            var commonfactor = GCD(numerator, denominator);

            numerator   /= commonfactor;
            denominator /= commonfactor;
            res.n        = numerator;
            res.d        = denominator;
            return(res);
        }
Exemplo n.º 4
0
 public override OrderedField Multiply(OrderedField f)
 {
     if (f is OrderedField)
     {
         ExactRational b     = f as ExactRational;
         var           thisn = this.n;
         var           thisd = this.d;
         var           thatn = b.n;
         var           thatd = b.d;
         var           c1    = GCD(thisn, thatd);
         var           c2    = GCD(thatn, thisd);
         thisn /= c1;
         thatd /= c1;
         thatn /= c2;
         thisd /= c2;
         return(ConstructExactRational(thisn * thatn, thatd * thisd));
     }
     throw new NotImplementedException();
 }