コード例 #1
0
        public int CompareTo(Fraction64 other)
        {
            int i = _wholeNumber.CompareTo(other._wholeNumber);

            if (i != 0)
            {
                return(i);
            }
            if (_wholeNumber == 0)
            {
                if (_numerator == 0 || other._numerator == 0 || _denominator == other._denominator)
                {
                    return(_numerator.CompareTo(other._numerator));
                }
            }
            else
            {
                if (_numerator == 0)
                {
                    return((other._numerator < (long)(int.MinValue)) ? -1 : ((other._numerator > 0L) ? 1 : 0));
                }
                if (other._numerator == 0)
                {
                    return((_numerator < (long)(int.MinValue)) ? -1 : ((_numerator > 0L) ? 1 : 0));
                }
            }

            long n1 = _numerator, d1 = _denominator, n2 = other._numerator, d2 = other._denominator;

            FractionUtil.ToCommonDenominator64(ref n1, ref d1, ref n2, ref d2);
            return(n1.CompareTo(n2));
        }
コード例 #2
0
        public Fraction64 Subtract(long wholeNumber, long numerator, long denominator)
        {
            if (numerator == 0 && wholeNumber == 0)
            {
                return((_denominator == 0) ? Fraction64.Zero : this);
            }

            long w1 = _wholeNumber, n1 = _numerator, d1 = _denominator, w2, n2 = numerator, d2 = denominator;

            w2 = FractionUtil.GetNormalizedRational64(wholeNumber, numerator, denominator, out n2, out d2);
            FractionUtil.ToCommonDenominator64(ref n1, ref d1, ref n2, ref d2);
            w1 = FractionUtil.GetNormalizedRational64(w1 + w2, n1 + n2, d1, out n1, out d1);
            return(new Fraction64((long)w1, (long)n1, (long)d1));
        }
コード例 #3
0
        private int CompareTo(IFraction <long> other)
        {
            if (other == null)
            {
                return(1);
            }

            if (other is Fraction64)
            {
                return(CompareTo((Fraction64)other));
            }

            long n, d;
            long w = FractionUtil.GetNormalizedRational64(other.WholeNumber, other.Numerator, other.Denominator, out n, out d);

            int i = _wholeNumber.CompareTo(w);

            if (i != 0)
            {
                return(i);
            }
            if (_wholeNumber == 0)
            {
                if (_numerator == 0 || n == 0 || _denominator == d)
                {
                    return(_numerator.CompareTo(n));
                }
            }
            else
            {
                if (_numerator == 0)
                {
                    return((n < (long)(int.MinValue)) ? -1 : ((n > 0L) ? 1 : 0));
                }
                if (n == 0)
                {
                    return((_numerator < (long)(int.MinValue)) ? -1 : ((_numerator > 0L) ? 1 : 0));
                }
            }

            long n1 = _numerator, d1 = _denominator, n2 = n, d2 = d;

            FractionUtil.ToCommonDenominator64(ref n1, ref d1, ref n2, ref d2);
            return(n1.CompareTo(n2));
        }
コード例 #4
0
        private int CompareTo(IFraction <int> other)
        {
            if (other == null)
            {
                return(1);
            }

            if (other is Fraction32)
            {
                return(CompareTo((Fraction32)other));
            }

            int n, d;
            int w = FractionUtil.GetNormalizedRational(other.WholeNumber, other.Numerator, other.Denominator, out n, out d);

            int i = _wholeNumber.CompareTo(w);

            if (i != 0)
            {
                return(i);
            }
            if (_wholeNumber == 0)
            {
                if (_numerator == 0 || n == 0 || _denominator == d)
                {
                    return(_numerator.CompareTo(n));
                }
            }
            else
            {
                if (_numerator == 0)
                {
                    return(n);
                }
                if (n == 0)
                {
                    return(_numerator);
                }
            }

            long n1 = _numerator, d1 = _denominator, n2 = n, d2 = d;

            FractionUtil.ToCommonDenominator64(ref n1, ref d1, ref n2, ref d2);
            return(n1.CompareTo(n2));
        }
コード例 #5
0
        public Fraction64 Add(Fraction64 other)
        {
            if (_numerator == 0 && _wholeNumber == 0)
            {
                return((other._denominator == 0) ? Fraction64.Zero : other);
            }

            if (other._numerator == 0 && other._wholeNumber == 0)
            {
                return((_denominator == 0) ? Fraction64.Zero : this);
            }

            long w1 = _wholeNumber, n1 = _numerator, d1 = _denominator, w2 = other._wholeNumber, n2 = other._numerator, d2 = other._denominator;

            FractionUtil.ToCommonDenominator64(ref n1, ref d1, ref n2, ref d2);
            w1 = FractionUtil.GetNormalizedRational64(w1 + w2, n1 + n2, d1, out n1, out d1);
            return(new Fraction64((long)w1, (long)n1, (long)d1));
        }
コード例 #6
0
        public Fraction32 Add(int wholeNumber, int numerator, int denominator)
        {
            if (_numerator == 0 && _wholeNumber == 0)
            {
                return(new Fraction32(wholeNumber, numerator, denominator));
            }

            if (numerator == 0 && wholeNumber == 0)
            {
                return((_denominator == 0) ? Fraction32.Zero : this);
            }

            long w1 = _wholeNumber, n1 = _numerator, d1 = _denominator, w2, n2 = numerator, d2 = denominator;

            w2 = FractionUtil.GetNormalizedRational64(wholeNumber, numerator, denominator, out n2, out d2);
            FractionUtil.ToCommonDenominator64(ref n1, ref d1, ref n2, ref d2);
            w1 = FractionUtil.GetNormalizedRational64(w1 + w2, n1 + n2, d1, out n1, out d1);
            return(new Fraction32((int)w1, (int)n1, (int)d1));
        }