コード例 #1
0
        public Fraction64(IFraction fraction)
        {
            long numerator, denominator;

            _wholeNumber = FractionUtil.GetNormalizedRational64(FractionUtil.ToInt64(fraction.WholeNumber), FractionUtil.ToInt64(fraction.Numerator), FractionUtil.ToInt64(fraction.Denominator, 1), out numerator, out denominator);
            _numerator   = numerator;
            _denominator = denominator;
        }
コード例 #2
0
        public Fraction64 Multiply(Fraction64 other)
        {
            if ((_numerator == 0 && _wholeNumber == 0) || (other._numerator == 0 && other._wholeNumber == 0))
            {
                return(Fraction64.Zero);
            }

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

            w1 = FractionUtil.GetNormalizedRational64(w1 * w2, n1 * n2, d1 * d2, out n1, out d1);
            return(new Fraction64((long)w1, (long)n1, (long)d1));
        }
コード例 #3
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));
        }
コード例 #4
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));
        }
コード例 #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 Fraction64 Multiply(long wholeNumber, long numerator, long denominator)
        {
            if ((_numerator == 0 && _wholeNumber == 0) || (numerator == 0 && wholeNumber == 0))
            {
                return(Fraction64.Zero);
            }

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

            w2 = FractionUtil.GetNormalizedRational64(wholeNumber, numerator, denominator, out n2, out d2);

            if (numerator == 0 && wholeNumber == 0)
            {
                return(Fraction64.Zero);
            }

            w1 = FractionUtil.GetNormalizedRational64(w1 * w2, n1 * n2, d1 * d2, out n1, out d1);
            return(new Fraction64((long)w1, (long)n1, (long)d1));
        }
コード例 #7
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));
        }
コード例 #8
0
        private bool Equals(IFraction <long> other)
        {
            if (other == null)
            {
                return(false);
            }

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

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

            if (_numerator == 0)
            {
                return(n == 0 && _wholeNumber == w);
            }

            return(_numerator == n && _denominator == d && _wholeNumber == w);
        }
コード例 #9
0
        public Fraction64 Divide(long wholeNumber, long numerator, long denominator)
        {
            if (_numerator == 0 && _wholeNumber == 0)
            {
                return(Fraction64.Zero);
            }

            if (numerator == 0 && wholeNumber == 0)
            {
                throw new DivideByZeroException();
            }

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

            w2 = FractionUtil.GetInvertedRational64(wholeNumber, numerator, denominator, out n2, out d2);

            if (n2 == 0 && w2 == 0)
            {
                throw new DivideByZeroException();
            }

            w1 = FractionUtil.GetNormalizedRational64(w1 * w2, n1 * n2, d1 * d2, out n1, out d1);
            return(new Fraction64((long)w1, (long)n1, (long)d1));
        }
コード例 #10
0
 public Fraction64(long numerator, long denominator)
 {
     _wholeNumber = FractionUtil.GetNormalizedRational64(0, numerator, denominator, out numerator, out denominator);
     _numerator   = numerator;
     _denominator = denominator;
 }