Ejemplo n.º 1
0
        /// <summary>
        /// Lowest Common Denominator
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private static decimal LCD(decimal a, decimal b)
        {
            if (a == Decimal.Zero && b == Decimal.Zero)
            {
                return(Decimal.Zero);
            }

            return((a * b) / Rational <T> .GCD(a, b));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Finds the greatest common divisor and reduces the fraction by this amount.
        /// </summary>
        /// <returns>the reduced rational</returns>
        private static void Reduce(ref T numerator, ref T denominator)
        {
            bool reduced = false;

            decimal n = Convert.ToDecimal(numerator);
            decimal d = Convert.ToDecimal(denominator);

            if (n == 1 && d == 1)
            {
                return;
            }

            // greatest commondivisor
            decimal gcd = Rational <T> .GCD(n, d);

            if (gcd != Decimal.One && gcd != Decimal.Zero)
            {
                reduced = true;
                n      /= gcd;
                d      /= gcd;
            }

            // cancel out signs
            if (d < Decimal.Zero)
            {
                reduced = true;
                n       = -n;
                d       = -d;
            }

            if (reduced)
            {
                numerator   = (T)Convert.ChangeType(n, typeof(T));
                denominator = (T)Convert.ChangeType(d, typeof(T));
            }
        }