Example #1
0
        public void TestAddition()
        {
            // 3/2 + 10/8 == 201/2
            BigRational threeHalfs = new BigRational(BigInteger.Zero, new Fraction(3, 2));
            BigRational tenEighths = new BigRational(BigInteger.Zero, new Fraction(10, 8));
            BigRational expected1  = BigRational.Reduce(new BigRational(BigInteger.Zero, new Fraction(11, 4)));

            // 1/100 + 1/2 == 11/4
            BigRational oneHundred = new BigRational(100);
            BigRational oneHalf    = new BigRational(BigInteger.Zero, new Fraction(1, 2));
            BigRational expected2  = BigRational.Reduce(new BigRational(BigInteger.Zero, new Fraction(201, 2)));

            // 1/3 + 1/5 == 1/2 + 1/30 == 8/15
            BigRational oneThird      = new BigRational(0, 1, 3);
            BigRational oneFifth      = new BigRational(0, new Fraction(1, 5));
            BigRational oneThirtieth  = new BigRational(0, new Fraction(1, 30));
            BigRational expected3and4 = BigRational.Reduce(new BigRational(BigInteger.Zero, new Fraction(8, 15)));

            // Calculate
            BigRational result1 = BigRational.Add(threeHalfs, tenEighths);
            BigRational result2 = BigRational.Add(oneHundred, oneHalf);
            BigRational result3 = BigRational.Add(oneThird, oneFifth);
            BigRational result4 = BigRational.Add(oneHalf, oneThirtieth);

            // Assert
            Assert.AreEqual(expected1, result1);
            Assert.AreEqual(expected2, result2);
            Assert.AreEqual(expected3and4, result3);
            Assert.AreEqual(expected3and4, result4);
        }
Example #2
0
        public void DivideBigRationalNumbersTest()
        {
            // Arrange
            const long  a         = 3;
            const long  b         = 4;
            BigRational rational1 = new(a, b);

            const long  c         = 5;
            const long  d         = 6;
            BigRational rational2 = new(c, d);

            BigInteger expectedNumerator   = a * d;
            BigInteger expectedDenominator = b * c;

            BigRational expectedQuotient        = new(expectedNumerator, expectedDenominator);
            BigRational reducedExpectedQuotient = BigRational.Reduce(expectedQuotient);

            BigInteger reducedExpectedNumerator   = reducedExpectedQuotient.Numerator;
            BigInteger reducedExpectedDenominator = reducedExpectedQuotient.Denominator;

            // Act
            BigRational quotient = BigRational.Divide(rational1, rational2);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.That(quotient.Numerator, Is.EqualTo(reducedExpectedNumerator));
                Assert.That(quotient.Denominator, Is.EqualTo(reducedExpectedDenominator));
            });
        }
Example #3
0
        public void AddBigRationalNumbersTest()
        {
            // Arrange
            const long  a         = 3;
            const long  b         = 4;
            BigRational rational1 = new(a, b);

            const long  c         = 5;
            const long  d         = 6;
            BigRational rational2 = new(c, d);

            BigInteger expectedNumerator   = a * d + b * c;
            BigInteger expectedDenominator = b * d;

            BigRational expectedSum        = new(expectedNumerator, expectedDenominator);
            BigRational reducedExpectedSum = BigRational.Reduce(expectedSum);

            BigInteger reducedExpectedNumerator   = reducedExpectedSum.Numerator;
            BigInteger reducedExpectedDenominator = reducedExpectedSum.Denominator;

            // Act
            BigRational sum = BigRational.Add(rational1, rational2);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.That(sum.Numerator, Is.EqualTo(reducedExpectedNumerator));
                Assert.That(sum.Denominator, Is.EqualTo(reducedExpectedDenominator));
            });
        }
Example #4
0
        public void TestDivision()
        {
            BigRational sevenTwoths = new BigRational(BigInteger.Zero, new Fraction(7, 2));
            BigRational sevenFifths = new BigRational(BigInteger.Zero, new Fraction(7, 5));

            BigRational expected = BigRational.Reduce(new BigRational(BigInteger.Zero, 5, 2));
            BigRational result   = BigRational.Divide(sevenTwoths, sevenFifths);

            Assert.AreEqual(expected, result);
        }
Example #5
0
        public void TestMultiplication()
        {
            BigRational sevenTwoths = new BigRational(BigInteger.Zero, new Fraction(7, 2));
            BigRational sevenFifths = new BigRational(BigInteger.Zero, new Fraction(7, 5));

            BigRational expected = BigRational.Reduce(new BigRational(BigInteger.Zero, 49, 10));
            BigRational result   = BigRational.Multiply(sevenTwoths, sevenFifths);

            Assert.AreEqual(expected, result);
        }
Example #6
0
        public void ReduceBigRationalNumberTest()
        {
            // Arrange
            const long  a        = 2 * 3 * 5 * 7;
            const long  b        = 5 * 7 * 11 * 13;
            BigRational rational = new(a, b);

            BigInteger expectedNumerator   = 6;
            BigInteger expectedDenominator = 143;

            // Act
            BigRational reducedRational = BigRational.Reduce(rational);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.That(reducedRational.Numerator, Is.EqualTo(expectedNumerator));
                Assert.That(reducedRational.Denominator, Is.EqualTo(expectedDenominator));
            });
        }
Example #7
0
        public void ReduceIrreducableBigRationalNumberTest()
        {
            // Arrange
            const long  a        = 12;
            const long  b        = 5;
            BigRational rational = new(a, b);

            BigInteger expectedNumerator   = a;
            BigInteger expectedDenominator = b;

            // Act
            BigRational reducedRational = BigRational.Reduce(rational);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.That(reducedRational.Numerator, Is.EqualTo(expectedNumerator));
                Assert.That(reducedRational.Denominator, Is.EqualTo(expectedDenominator));
            });
        }