コード例 #1
0
 /// <summary>
 /// Compares two
 /// <see cref="Rational"/>
 /// instances, returning true if they are mathematically
 /// equivalent.
 /// </summary>
 /// <param name="obj">
 /// the
 /// <see cref="Rational"/>
 /// to compare this instance to.
 /// </param>
 /// <returns>
 /// true if instances are mathematically equivalent, otherwise false.  Will also
 /// return false if <code>obj</code> is not an instance of
 /// <see cref="Rational"/>
 /// .
 /// </returns>
 public override bool Equals(object obj)
 {
     if (obj == null || !(obj is Com.Drew.Lang.Rational))
     {
         return(false);
     }
     Com.Drew.Lang.Rational that = (Com.Drew.Lang.Rational)obj;
     return(this.DoubleValue() == that.DoubleValue());
 }
コード例 #2
0
 public virtual string ToSimpleString(bool allowDecimal)
 {
     if (_denominator == 0 && _numerator != 0)
     {
         return(ToString());
     }
     else
     {
         if (IsInteger())
         {
             return(Sharpen.Extensions.ToString(IntValue()));
         }
         else
         {
             if (_numerator != 1 && _denominator % _numerator == 0)
             {
                 // common factor between denominator and numerator
                 long newDenominator = _denominator / _numerator;
                 return(new Com.Drew.Lang.Rational(1, newDenominator).ToSimpleString(allowDecimal));
             }
             else
             {
                 Com.Drew.Lang.Rational simplifiedInstance = GetSimplifiedInstance();
                 if (allowDecimal)
                 {
                     string doubleString = simplifiedInstance.DoubleValue().ToString();
                     if (doubleString.Length < 5)
                     {
                         return(doubleString);
                     }
                 }
                 return(simplifiedInstance.ToString());
             }
         }
     }
 }