コード例 #1
0
        private static T GetSimplifiedRational <T>(IValueHelper <T> valueHelper, T n, T d, out T denominator)
            where T : struct, IComparable, IFormattable, IConvertible, IComparable <T>, IEquatable <T>
        {
            if (d.Equals(valueHelper.Zero))
            {
                throw new DivideByZeroException();
            }

            if (n.Equals(valueHelper.Zero))
            {
                denominator = valueHelper.PositiveOne;
                return(valueHelper.Zero);
            }

            if (n.Equals(d))
            {
                denominator = valueHelper.PositiveOne;
                return(valueHelper.PositiveOne);
            }

            if (d.CompareTo(valueHelper.Zero) < 0)
            {
                d = valueHelper.Multiply(d, valueHelper.NegativeOne);
                n = valueHelper.Multiply(n, valueHelper.NegativeOne);
            }
            T gcd = GetGCD <T>(valueHelper, n, d);

            denominator = valueHelper.Divide(d, gcd);
            return(valueHelper.Divide(n, gcd));
        }
コード例 #2
0
        private static void ToCommonDenominator <T>(IValueHelper <T> valueHelper, ref T n1, ref T d1, ref T n2, ref T d2)
            where T : struct, IComparable, IFormattable, IConvertible, IComparable <T>, IEquatable <T>
        {
            if (d1.Equals(valueHelper.Zero) || d2.Equals(valueHelper.Zero))
            {
                throw new DivideByZeroException();
            }

            if (n1.Equals(valueHelper.Zero))
            {
                d1 = d2;
            }
            else if (n2.Equals(valueHelper.Zero))
            {
                d2 = d1;
            }
            else if (!d1.Equals(d2))
            {
                n1 = GetSimplifiedRational <T>(valueHelper, n1, d1, out d1);
                n2 = GetSimplifiedRational <T>(valueHelper, n2, d2, out d2);

                if (d1.Equals(valueHelper.PositiveOne))
                {
                    n1 = valueHelper.Multiply(n1, d2);
                }
                else if (d2.Equals(valueHelper.PositiveOne))
                {
                    n2 = valueHelper.Multiply(n2, d1);
                }
                else if (!d1.Equals(d2))
                {
                    T m2;
                    T m1 = GetLCM <T>(valueHelper, d1, d2, out m2);
                    n1 = valueHelper.Multiply(n1, m1);
                    d1 = valueHelper.Multiply(d1, m1);
                    n2 = valueHelper.Multiply(n2, m1);
                    d2 = valueHelper.Multiply(d2, m1);
                }
            }
        }
コード例 #3
0
        private static T GetInvertedRational <T>(IValueHelper <T> valueHelper, T w, T n, T d, out T numerator, out T denominator)
            where T : struct, IComparable, IFormattable, IConvertible, IComparable <T>, IEquatable <T>
        {
            w = GetNormalizedRational <T>(valueHelper, w, n, d, out numerator, out denominator);

            if (n.Equals(valueHelper.Zero))
            {
                if (w.Equals(valueHelper.Zero))
                {
                    numerator = n;
                    return(w);
                }

                return(GetNormalizedRational <T>(valueHelper, valueHelper.Zero, valueHelper.PositiveOne, w, out numerator, out denominator));
            }

            if (w.Equals(valueHelper.Zero))
            {
                return(GetNormalizedRational <T>(valueHelper, valueHelper.Zero, d, n, out numerator, out denominator));
            }

            return(GetNormalizedRational <T>(valueHelper, valueHelper.Zero, d, valueHelper.Add(n, valueHelper.Multiply(d, w)), out numerator, out denominator));
        }