예제 #1
0
        private static BigRational Multiply(BigRational x, BigRational y)
        {
            // a/b * c/d == (ac/bd)

            BigInt a = x._num;
            BigInt b = x._den;
            BigInt c = y._num;
            BigInt d = y._den;

            BigInt newNum = (BigInt)(a * c);
            BigInt newDen = (BigInt)(b * d);

            Normalise(newNum, newDen, out newNum, out newDen);

            return(new BigRational(newNum, newDen));
        }
예제 #2
0
        private static BigRational Divide(BigRational x, BigRational y)
        {
            // a/b / c/d == a/b * d/c == (ad/bc)

            BigInt a = x._num;
            BigInt b = x._den;
            BigInt c = y._num;
            BigInt d = y._den;

            BigInt newNum = (BigInt)(a * b);
            BigInt newDen = (BigInt)(b * c);

            Normalise(newNum, newDen, out newNum, out newDen);

            return(new BigRational(newNum, newDen));
        }
예제 #3
0
        public BigRational(String num, int nBase)
        {
            // the algo to convert from a float into a rational isn't that complicated:

            // 1. Get the non-integer part of the number
            // 2. Assuming it's base 10, then:
            // 2.1 ...the numerator is the non-integer part converted to an integer, and the
            // 2.2 ...the denominator is an integer equal to 10^(length of non-integer part)
            // 2.3 ...then just normalise it
            // 3. Then you need to add the integer part back


            // HACK: String processing to get numbers: baaaaad

            int radixIdx = num.IndexOf('.');

            if (radixIdx == -1)
            {
                _num = new BigInt(num);
                _den = new BigInt(1);
                return;
            }

            String intPart = num.Substring(0, radixIdx);
            String fltPart = num.Substring(radixIdx);

            BigInt tempNumerator   = new BigInt(fltPart);
            BigInt tempDenominator = new BigInt((int)Math.Pow(nBase, fltPart.Length));

            Normalise(tempNumerator, tempDenominator, out tempNumerator, out tempDenominator);

            // then add back the integer part

            BigRational intPortion = new BigRational(intPart);
            BigRational fltPortion = new BigRational(tempNumerator, tempDenominator);

            BigRational result = (BigRational)(intPortion + fltPortion);

            this._num = result._num;
            this._den = result._den;
        }
예제 #4
0
        public BigRational(String num, int nBase)
        {
            // the algo to convert from a float into a rational isn't that complicated:

            // 1. Get the non-integer part of the number
            // 2. Assuming it's base 10, then:
            // 2.1 ...the numerator is the non-integer part converted to an integer, and the
            // 2.2 ...the denominator is an integer equal to 10^(length of non-integer part)
            // 2.3 ...then just normalise it
            // 3. Then you need to add the integer part back

            // HACK: String processing to get numbers: baaaaad

            int radixIdx = num.IndexOf('.');

            if( radixIdx == -1 ) {

                _num = new BigInt( num );
                _den = new BigInt(1);
                return;

            }

            String intPart = num.Substring(0, radixIdx);
            String fltPart = num.Substring( radixIdx );

            BigInt tempNumerator   = new BigInt( fltPart );
            BigInt tempDenominator = new BigInt( (int)Math.Pow( nBase, fltPart.Length ) );

            Normalise( tempNumerator, tempDenominator, out tempNumerator, out tempDenominator );

            // then add back the integer part

            BigRational intPortion = new BigRational( intPart );
            BigRational fltPortion = new BigRational( tempNumerator, tempDenominator );

            BigRational result = (BigRational)(intPortion + fltPortion);

            this._num = result._num;
            this._den = result._den;
        }
예제 #5
0
        private static BigRational Add(BigRational x, BigRational y)
        {
            // the easiest way to do operations is by getting the simple denominator product
            // ...then normalising later

            // I think computing the LCM/GCD at this stage is premature and might hinder performance

            // a/b + c/d == (a*d)/(b*d) + (c*b)/(d*b) == (a*d)+(c*d) / (b*d)

            BigInt a = x._num;
            BigInt b = x._den;
            BigInt c = y._num;
            BigInt d = y._den;

            BigInt newNum = (BigInt)(a * d + c * d);
            BigInt newDen = (BigInt)(b * d);

            // then normalise

            Normalise(newNum, newDen, out newNum, out newDen);

            return(new BigRational(newNum, newDen));
        }
예제 #6
0
        private static BigRational Multiply(BigRational x, BigRational y)
        {
            // a/b * c/d == (ac/bd)

            BigInt a = x._num;
            BigInt b = x._den;
            BigInt c = y._num;
            BigInt d = y._den;

            BigInt newNum = (BigInt)( a * c );
            BigInt newDen = (BigInt)( b * d );

            Normalise(newNum, newDen, out newNum, out newDen);

            return new BigRational(newNum, newDen);
        }
예제 #7
0
        private static BigRational Divide(BigRational x, BigRational y)
        {
            // a/b / c/d == a/b * d/c == (ad/bc)

            BigInt a = x._num;
            BigInt b = x._den;
            BigInt c = y._num;
            BigInt d = y._den;

            BigInt newNum = (BigInt)( a * b );
            BigInt newDen = (BigInt)( b * c );

            Normalise(newNum, newDen, out newNum, out newDen);

            return new BigRational(newNum, newDen);
        }
예제 #8
0
        private static BigRational Add(BigRational x, BigRational y)
        {
            // the easiest way to do operations is by getting the simple denominator product
            // ...then normalising later

            // I think computing the LCM/GCD at this stage is premature and might hinder performance

            // a/b + c/d == (a*d)/(b*d) + (c*b)/(d*b) == (a*d)+(c*d) / (b*d)

            BigInt a = x._num;
            BigInt b = x._den;
            BigInt c = y._num;
            BigInt d = y._den;

            BigInt newNum = (BigInt)(a * d + c * d);
            BigInt newDen = (BigInt)(b * d);

            // then normalise

            Normalise(newNum, newDen, out newNum, out newDen);

            return new BigRational(newNum, newDen);
        }