Пример #1
0
        /// <summary>
        /// Add and normalize
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Rational <T, TPolicy> operator +(Rational <T, TPolicy> a, Rational <T, TPolicy> b)
        {
            if (Policy.IsZero(a.Numerator))
            {
                return(b);
            }
            if (Policy.IsZero(b.Numerator))
            {
                return(a);
            }

            if (Policy.IsOne(a.Denominator) && Policy.IsOne(b.Denominator))
            {
                return(new Rational <T, TPolicy>(Policy.Add(a.Numerator, b.Numerator), a.Denominator));
            }

            var d  = Policy.Lcd(a.Denominator, b.Denominator);
            var an = Policy.Mul(a.Numerator, (Policy.Div(d, a.Denominator)));
            var bn = Policy.Mul(b.Numerator, (Policy.Div(d, b.Denominator)));

            return(new Rational <T, TPolicy>(Policy.Add(an, bn), d).Normalize());
        }