Exemplo n.º 1
0
        /// <summary>
        /// Works fine, after moderate testing.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static ProperFraction operator *(ProperFraction a, ProperFraction b)
        {
            if (a == null || b == null)
            {
                throw new Exception();
            }
            Int64 aN = a.decimalpart.getNumerator(), aD = a.decimalpart.getDenominator();
            Int64 bN = b.decimalpart.getNumerator(), bD = b.decimalpart.getDenominator();
            Int64 aI = a.integerpart, bI = b.integerpart;

            //Covert to improper fraction to elimiate integer part.
            aN += aI * aD; bN += bI * bD;

            //Does numerator of a share factor with denominator of b?
            Int64 factor1 = ImproperFraction.GCD(aN, bD);

            aN /= factor1; bD /= factor1;

            //Does the numerator of b share factor with the denominator of a?
            Int64 factor2 = ImproperFraction.GCD(bN, aD);

            bN /= factor2; aD /= factor2;

            //take care of the sign:
            bool negative = a.sign != b.sign;

            if (negative)
            {
                aN = -aN;
            }

            return(new ProperFraction(aN * bN, aD * bD));
        }
Exemplo n.º 2
0
        /// <summary>
        /// construct an instance of proper fraction by inputing numerator and denominator.
        ///
        /// </summary>
        /// <param name="n"></param>
        /// Numerator.
        /// <param name="d"></param>
        /// Denominator
        /// If this is zero, then this class will throw an zero
        /// denominator exception.
        ///
        public ProperFraction(Int64 n, Int64 d)
        {
            if (d == 0)
            {
                throw new DividebyZeroException();
            }
            if (n == 0)
            {
                this.integerpart = 0;
                decimalpart      = new ImproperFraction(0, Math.Abs(d));
                return;
            }

            sign = n < 0 == d < 0;
            n    = Math.Abs(n); d = Math.Abs(d);

            Int64 integerdivide = n / d;

            integerpart = integerdivide;
            n           = n % d;

            decimalpart = new ImproperFraction(n, d);
        }