Example #1
0
        public void TestDivisionNegative()
        {
            BigRational expected = BigRational.One;
            BigRational result   = BigRational.Divide(BigRational.MinusOne, BigRational.MinusOne);

            Assert.AreEqual(expected, result);
        }
    public static BigRational ComputeP(int k, int n, int p)
    {
        // the P(n) equation
        BigRational pnNumerator   = BigRational.Pow(p, n);
        BigRational pnDenominator = BigRational.Pow(k, (n - k)) * Factorial(k);


        // the P(0) equation

        //---the right side of "+" sign in Denominator
        BigRational pk         = BigRational.Pow(p, k);
        BigRational factorialK = Factorial(k);
        // CHANGED: Don't cast to double here (loses precision)
        BigRational lastPart             = (BigRational.Subtract(1, BigRational.Divide(p, k)));
        BigRational factorialAndLastPart = BigRational.Multiply(factorialK, lastPart);
        BigRational fullRightSide        = BigRational.Divide(pk, factorialAndLastPart);
        //---the left side of "+" sign in Denominator
        BigRational series = Series(k, p);


        BigRational p0Denominator = series + fullRightSide;
        BigRational p0Result      = BigRational.Divide(1, p0Denominator);

        BigRational pNResult = BigRational.Divide((pnNumerator * p0Result), pnDenominator);

        return(pNResult);
    }
Example #3
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 #4
0
    private static BigRational CalculateEulerNumber(object threadIndexObject)
    {
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        int         threadIndex = Convert.ToInt32(threadIndexObject);
        BigRational sum         = new BigRational(0.0m);

        for (int k = threadIndex; k < elementsCount; k += threadsCount)
        {
            BigRational numerator   = BigRational.Pow((3 * k), 2) + 1;
            BigRational denominator = Factorial(3 * k);
            sum += BigRational.Divide(numerator, denominator);
        }
        stopwatch.Stop();
        int threadNumber = threadIndex + 1;

        Console.WriteLine("Ğ¢hread " + threadNumber + ": ");
        Console.WriteLine("Time elapsed - " + stopwatch.Elapsed);
        if (!isInQuietMode)
        {
            Console.WriteLine("Intermediate sum - " + sum.ToString());
        }
        Console.WriteLine();
        return(sum);
    }
Example #5
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);
        }
    // CHANGED: Removed n as parameter (n just the index of summation here)
    public static BigRational Series(int k, int p)
    {
        BigRational series = new BigRational(0.0);
        var         fin    = k - 1;

        // CHANGED: Should be <= fin (i.e. <= k-1, or < k) because it's inclusive counting
        for (int i = 0; i <= fin; i++)
        {
            var power = BigRational.Pow(p, i);
            // CHANGED: was Factorial(n), should be factorial of n value in this part of the sequence being summed.
            var factorialN = Factorial(i);
            var sum        = BigRational.Divide(power, factorialN);
            series += sum;
        }
        return(series);
    }
        public void big_rational_static_methods()
        {
            Assert.True(0.5 == BigRational.Abs(0.5d));
            Assert.True(0.5 == BigRational.Abs(-0.5d));
            Assert.False(0.5 == BigRational.Abs(-1));
            Assert.False(0.5 == BigRational.Abs(1));

            Assert.True(-0.5 == BigRational.Negate(0.5d));
            Assert.True(0.5 == BigRational.Negate(-0.5d));
            Assert.False(0.5 == BigRational.Negate(-1));
            Assert.False(-0.5 == BigRational.Negate(1));

            //Doesn't work great... testing with equals doubles fails
            Assert.True(new BigRational(1, 2) == BigRational.Invert(new BigRational(2, 1)));
            Assert.True(new BigRational(2, 1) == BigRational.Invert(new BigRational(1, 2)));
            Assert.True(new BigRational(-1, 2) == BigRational.Invert(new BigRational(-2, 1)));
            Assert.True(new BigRational(-2, 1) == BigRational.Invert(new BigRational(-1, 2)));

            Assert.True(new BigRational(BigInteger.One) + new BigRational(BigInteger.One) == 2);
            Assert.True(BigRational.Add(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 2);

            //Not working for me
            //BigRational big1 = new BigRational(1.0);
            //big1++;
            //Assert.AreEqual(new BigRational(2, 1), big1);

            //Not working for me
            //BigRational big2 = new BigRational(1.0);
            //big2--;
            //Assert.True(0 == big2);

            Assert.True(new BigRational(BigInteger.One) - new BigRational(BigInteger.One) == 0);
            Assert.True(BigRational.Subtract(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 0);

            BigRational big3 = new BigRational(1.0);

            Assert.True(-1.0 == -big3);

            BigRational big4 = new BigRational(1.0);

            Assert.True(1.0 == +big3);

            Assert.True(new BigRational(BigInteger.One) * new BigRational(BigInteger.One) == 1);
            Assert.True(BigRational.Multiply(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 1);
            Assert.True(new BigRational(BigInteger.One) * new BigRational(2.0) == 2.0);
            Assert.True(BigRational.Multiply(new BigRational(BigInteger.One), new BigRational(2.0)) == 2.0);

            Assert.True(new BigRational(BigInteger.One) / new BigRational(BigInteger.One) == 1);
            Assert.True(BigRational.Divide(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 1);
            Assert.AreEqual(new BigRational(1, 2), new BigRational(new BigInteger(1)) / new BigRational(new BigInteger(2)));                    //Again, mixing consructors doesn't work well.
            Assert.AreEqual(new BigRational(1, 2), BigRational.Divide(new BigRational(new BigInteger(1)), new BigRational(new BigInteger(2)))); //Again, mixing consructors doesn't work well. Testing with equals double 0.5 fails

            //Assert.True(new BigRational(BigInteger.One) < new BigRational(2.0)); //Don't mix the constructors for comparison!!!
            Assert.True(new BigRational(1.0) < new BigRational(2.0));
            Assert.False(new BigRational(2.0) < new BigRational(1.0));
            Assert.False(new BigRational(1.0) < new BigRational(1.0));
            Assert.True(new BigRational(1.0) <= new BigRational(2.0));
            Assert.True(new BigRational(1.0) <= new BigRational(1.0));
            Assert.False(new BigRational(2.0) <= new BigRational(1.0));
            Assert.True(new BigRational(1.0) == new BigRational(1.0));
            Assert.True(new BigRational(1.0) != new BigRational(2.0));

            Assert.False(new BigRational(1.0) > new BigRational(2.0));
            Assert.True(new BigRational(2.0) > new BigRational(1.0));
            Assert.False(new BigRational(BigInteger.One) > new BigRational(BigInteger.One));
            Assert.False(new BigRational(1.0) >= new BigRational(2.0));
            Assert.True(new BigRational(BigInteger.One) >= new BigRational(BigInteger.One));
            Assert.True(new BigRational(2.0) >= new BigRational(1.0));

            //Modulus doesn't seem to work well
            //Assert.AreEqual(BigRational.Zero, new BigRational(2.0) % new BigRational(1.0));
            //Assert.AreEqual(BigRational.One, new BigRational(3.0) % new BigRational(2.0));
            //Assert.True(new BigRational(1.5) % new BigRational(1.0) == new BigRational(0.5));
            //Assert.True(new BigRational(1.0) % new BigRational(2.0) == 0);
        }