Example #1
0
        public static Fraction operator +(Fraction l, Fraction r)
        {
            if (l.Numerator == Empty.Numerator || l.Denominator == Empty.Denominator)
                if (r.Numerator == Empty.Numerator || r.Denominator == Empty.Denominator)
                {
                    Fraction f = 0;
                    return Empty;
                }
                else
                    return r;
            if (r.Numerator == Empty.Numerator || r.Denominator == Empty.Denominator)
                return l;

            uint CommonDenominator = Common.LCM(l.Denominator, r.Denominator);
            Fraction fr = new Fraction(l.Numerator * CommonDenominator / l.Denominator
                                        + r.Numerator * CommonDenominator / r.Denominator,
                                        CommonDenominator);
            return fr.Cancel();
        }
Example #2
0
 public void ToMixedNumber(out long WholePart, out Fraction FractionalPart)
 {
     if (this.Numerator == Empty.Numerator || this.Denominator == Empty.Denominator)
     {
         WholePart = 0;
         FractionalPart = Empty;
         return;
     }
     bool neg = this.Numerator < 0;
     WholePart = this.Numerator / this.Denominator;
     FractionalPart = (new Fraction(Math.Abs(this.Numerator) - Math.Abs(WholePart * this.Denominator), this.Denominator)).Cancel();
     if (WholePart == 0 && neg)
         FractionalPart.Numerator *= -1;
 }
Example #3
0
        public static Fraction operator *(Fraction l, Fraction r)
        {
            if (l.Numerator == Empty.Numerator || l.Denominator == Empty.Denominator)
                if (r.Numerator == Empty.Numerator || r.Denominator == Empty.Denominator)
                    return Empty;
                else
                    return r;
            if (r.Numerator == Empty.Numerator || r.Denominator == Empty.Denominator)
                return l;

            Fraction fr = new Fraction(l.Numerator * r.Numerator,
                                        l.Denominator * r.Denominator);
            return fr.Cancel();
        }
Example #4
0
 public static bool TryParse(string str, out Fraction f)
 {
     try
     {
         f = Parse(str);
         return true;
     }
     catch
     {
         f = new Fraction(0,1);
         return false;
     }
 }
Example #5
0
        public Fraction Reverse()
        {
            if (num == 0) throw new DivideByZeroException("denominator");

            Fraction f = new Fraction();

            if (den == Empty.Denominator) f.Numerator = Empty.Numerator;
            else f.Numerator = (num < 0) ? -den : den;

            if (num == Empty.Numerator) f.Denominator = Empty.Denominator;
            else f.Denominator = (uint)Math.Abs(num);

            return f;
        }
Example #6
0
 public static bool IsEmpty(Fraction f)
 {
     return f.Numerator == Fraction.Empty.Numerator || f.Denominator == Fraction.Empty.Denominator;
 }
Example #7
0
        public static Fraction FromMixedNumber(long wholePart, Fraction fractionalPart)
        {
            if (fractionalPart.Numerator == Fraction.Empty.Numerator || fractionalPart.Denominator == Fraction.Empty.Denominator)
                return Fraction.Empty;

            bool neg = wholePart < 0 || fractionalPart.Numerator < 0;
            if (wholePart == 0 && neg) fractionalPart *= -1;
            if (wholePart < 0) wholePart *= -1;
            Fraction f = wholePart + fractionalPart;
            if (neg) f *= -1;
            return f;
        }
Example #8
0
 private Angle(Fraction f)
 {
     val = f;
 }
Example #9
0
 public Angle(uint degrees, uint minutes, uint seconds, int sign)
 {
     val = degrees + new Fraction(minutes, 60) + new Fraction(seconds, 3600);
     if (sign < 0) val = -val;
 }
Example #10
0
 public Angle(uint degrees, int sign)
 {
     val = degrees;
     if (sign < 0) val = -val;
 }