Beispiel #1
0
        /// <summary>
        /// Method to divide two BigFraction objects
        /// </summary>
        /// <param name="b">BigFraction object being divided from the current object</param>
        /// <returns>result of division in its simplest form</returns>
        public BigFraction Divide(BigFraction b)
        {
            /*When you divide a fraction by another fraction we multiply the dividend by the reciprocal of the divisor
             * So, we will multiply the numerator of a with denominator of b and denominator of a with numerator of b
             */
            BigInteger numeratorB   = GetNumerator(b.ToString());
            BigInteger denominatorB = GetDenominator(b.ToString());

            //Negate the numerator and denominator if numerator is negative
            if (numeratorB.Sign == -1)
            {
                numeratorB   = -numeratorB;
                denominatorB = -denominatorB;
            }

            BigInteger finalNumerator   = BigInteger.One;
            BigInteger finalDenominator = BigInteger.One;

            if (numeratorB.IsZero)
            {
                finalNumerator   = BigInteger.Zero;
                finalDenominator = BigInteger.One;
            }
            else
            {
                finalNumerator   = this.numerator * denominatorB;
                finalDenominator = this.denominator * numeratorB;
            }
            BigFraction result = new BigFraction(finalNumerator.ToString() + "/" + finalDenominator.ToString());

            return(GetSimplestForm(result));
        }
Beispiel #2
0
        /// <summary>
        /// Method to multiply two BigFraction datatypes
        /// </summary>
        /// <param name="b">BigFraction being multiplied from current BigFraction</param>
        /// <returns>result of multiplication in its simplest form</returns>
        public BigFraction Multiply(BigFraction b)
        {
            BigInteger numeratorB   = GetNumerator(b.ToString());
            BigInteger denominatorB = GetDenominator(b.ToString());

            BigInteger finalNumerator   = this.numerator * numeratorB;
            BigInteger finalDenominator = this.denominator * denominatorB;

            BigFraction result = new BigFraction(finalNumerator.ToString() + "/" + finalDenominator.ToString());

            return(GetSimplestForm(result));
        }
Beispiel #3
0
        /// <summary>
        /// Method to subtract two BigFraction datatypes
        /// </summary>
        /// <param name="b">BigFraction being subtracted from current BigFraction</param>
        /// <returns>result of subtraction in its simplest form</returns>
        public BigFraction Subtract(BigFraction b)
        {
            BigInteger numeratorB   = GetNumerator(b.ToString());
            BigInteger denominatorB = GetDenominator(b.ToString());

            BigInteger lcm            = GetLCM(this.denominator, denominatorB);
            BigInteger finalNumerator = this.numerator * (lcm / this.denominator) -
                                        numeratorB * (lcm / denominatorB);

            BigFraction result = new BigFraction(finalNumerator.ToString() + "/" + lcm.ToString());

            return(GetSimplestForm(result));
        }
Beispiel #4
0
        /// <summary>
        /// Method to get the fraction in its simplest form
        /// </summary>
        /// <param name="frac"></param>
        /// <returns></returns>
        private BigFraction GetSimplestForm(BigFraction frac)
        {
            BigInteger num  = GetNumerator(frac.ToString());
            BigInteger deno = GetDenominator(frac.ToString());
            BigInteger gcf  = GetGCF(num, deno);

            while (!gcf.IsOne)
            {
                num  = num / gcf;
                deno = deno / gcf;
                gcf  = GetGCF(num, deno);
            }

            return(new BigFraction(num.ToString() + "/" + deno.ToString()));
        }