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);
        }
 public static void Main1(string[] args)
 {
     Stopwatch stopwatch = new Stopwatch();
     stopwatch.Start();
     List<Task<BigRational>> tasks = new List<Task<BigRational>>();
     //Create the tasks
     for (int threadIndex = 0; threadIndex < threadsCount; threadIndex++)
     {
         Task<BigRational> task = new Task<BigRational>((data)=>
         {
             return CalculateEulerNumber(data);
             
         },threadIndex);
         tasks.Add(task);
     }
     foreach (var task in tasks)
     {
         task.Start();
     }
     //Wait for tasks
     Task.WaitAll(tasks.ToArray());
     //Add the results
     foreach (var task in tasks)
     {
         totalSum = BigRational.Add(totalSum, task.Result); 
     }
     File.WriteAllText(outputFileName, "Euler's number: " + totalSum);
     stopwatch.Stop();
     Console.WriteLine("Result: ");
     Console.WriteLine("Total time elapsed - " + stopwatch.Elapsed);
     if (!isInQuietMode)
     {
         Console.WriteLine("Euler's number - " + totalSum);
     }
 }
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));
            });
        }
        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);
        }
 /// <summary>
 /// Adds <paramref name="amount"/> of the security <paramref name="security"/> to this portfolio and returns the new value
 /// Positive values add to the portfolio, while negative numbers subtract from
 /// Does not protect against negative ending values or validate transact
 /// </summary>
 /// <param name=""></param>
 /// <returns>New value for this security</returns>
 public BigRational ForceTransact(Security security, BigRational amount)
 {
     return(AvailableSecurities[security] = BigRational.Add(Get(security), amount));
 }