Example #1
0
	/// <summary>
    /// Method to add the two BigFraction datatypes
    /// </summary>
    /// <param name="b">BigFraction being added to the current BigFraction</param>
    /// <returns>result of the addition in the simplest form</returns>
	public BigFraction Add(BigFraction b)
    {
        BigInteger numeratorB = GetNumerator(b.ToString());
        BigInteger denominatorB = GetDenominator(b.ToString());


    }
        /// <summary>
        /// Разбивает дробь на левую и правую отзнака неравенства части
        /// </summary>
        /// <param name="generationKey">ключ генерации</param>
        void GenerateLeftAndRightSides(GenerationKey generationKey)
        {
            Random rnd = new Random(generationKey.Seed);

            LeftSide  = new List <Fraction>();
            RightSide = new List <Fraction>();
            List <Fraction> fracList = BigFraction.DecomposeAmountOfFractions(generationKey);

            if (fracList.Count() == 1)
            {
                fracList[0].MultiplyPolynominal(generationKey.Seed, settings);
            }
            for (int i = 0; i < fracList.Count; i++)
            {
                int rndValue = rnd.Next(2);
                if (rndValue == 0)
                {
                    LeftSide.Add(fracList[i]);
                }
                else
                {
                    RightSide.Add(-fracList[i]);
                }
            }
        }
        public void DouConstructor()
        {
            BigFraction a        = new BigFraction(6545.99);
            BigFraction expected = new BigFraction(new BigInteger(654599), new BigInteger(100));

            Assert.Equal(expected, a);
        }
Example #4
0
    public static void TestFraction()
    {
        BigFraction a = new BigFraction(2, 3);
        BigFraction b = new BigFraction(3, 4);

        // 测试加减乘除
        Console.WriteLine(a + b);
        Console.WriteLine(a - b);
        Console.WriteLine(a * b);
        Console.WriteLine(a / b);

        Console.WriteLine(a + 1);
        Console.WriteLine(a - 1);
        Console.WriteLine(a * 1);
        Console.WriteLine(a / 1);

        Console.WriteLine(1 + a);
        Console.WriteLine(1 - a);
        Console.WriteLine(1 * a);
        Console.WriteLine(1 / a);

        // 测试隐式转换
        Console.WriteLine(0.1f + (float)a);
        Console.WriteLine(0.1d + (double)b);
    }
        public void IntConstructor()
        {
            BigFraction a        = new BigFraction(-10);
            BigFraction expected = new BigFraction(new BigInteger(-10), BigInteger.One);

            Assert.Equal(expected, a);
        }
Example #6
0
        public void TestLargeFractionToDouble()
        {

            BigInteger i = 10;
            i = BigInteger.Pow(i, 304);
            BigFraction f = i;

            Assert.AreEqual(1e304, (double)f);

            Assert.AreEqual(1e40, (double)
                new BigFraction(BigInteger.Pow(10, 40), 1), 1e25);

            BigFraction f2 = new BigFraction(BigInteger.Pow(10, 344), BigInteger.Pow(10, 304), false);

            double d2 = (double)f2;

            Assert.AreEqual(1e40, d2, 1e25);

            BigInteger bi1 = BigInteger.Parse("2302073370472459132074404233276820422639103254133904159801405804887478238801437662513031371866270764870331175412196858812068470158590547389959356493994877674610281421320525996469152129835615048054927204470044026763562485477222367114215047175691226850045987011995428311814345330046987446489172747558771614920732");
            BigInteger bi2 = BigInteger.Parse("17365835781473320889421419340788343549711913213696800725957886198888610396662017817843102760805884240481836360411987064468925009319623959418836710583418588884564675920797412194729605191255404903006715915289897693739972657264720773841442988736074423009480659734946421941204433341642671302399787605534020996634264");

            double expected = .132563350214817635401;
            double f3 = (double)new BigFraction(bi1, bi2);
            Assert.IsFalse(Double.IsNaN(f3));
            Assert.AreEqual(expected, f3, 1e-9);
        }
Example #7
0
        public void TestNegativeFloatingPoint()
        {
            {
                var algorithm = new Paillier
                {
                    KeySize = 384
                };

                //Test 0 > plaintext > -1
                var z           = new BigFraction(BigInteger.Parse("-1001"), BigInteger.Parse("100"));
                var z_enc_bytes = algorithm.EncryptData(z);
                var z_dec       = algorithm.DecryptData(z_enc_bytes);
                Assert.Equal(z, z_dec);

                //Test plaintext < -1
                var z_2           = new BigFraction(BigInteger.Parse("-1000000001"), BigInteger.Parse("100"));
                var z_2_enc_bytes = algorithm.EncryptData(z_2);
                var z_2_dec       = algorithm.DecryptData(z_2_enc_bytes);
                Assert.Equal(z_2, z_2_dec);

                //Test addition
                var z_enc_addition = algorithm.Addition(z_enc_bytes, z_2_enc_bytes);
                var z_addition     = algorithm.DecryptData(z_enc_addition);
                Assert.Equal(z + z_2, z_addition);

                algorithm.Dispose();
            }
        }
        public void Equal()
        {
            BigFraction a = new BigFraction(-251.15);
            BigFraction b = new BigFraction(-251.15);

            Assert.True(a == b);
        }
        public void SmallerEqual()
        {
            BigFraction a = new BigFraction(-251.15);
            BigFraction b = new BigFraction(4.20);

            Assert.True(a <= b);
        }
Example #10
0
        public void GreaterEqual()
        {
            BigFraction a = new BigFraction(251.15);
            BigFraction b = new BigFraction(4.20);

            Assert.True(a >= b);
        }
Example #11
0
        public void Greater()
        {
            BigFraction a = new BigFraction(5.25);
            BigFraction b = new BigFraction(4.20);

            Assert.True(a > b);
        }
Example #12
0
        public void LongConstructor()
        {
            BigFraction a        = new BigFraction(12147483647);
            BigFraction expected = new BigFraction(new BigInteger(12147483647), BigInteger.One);

            Assert.Equal(expected, a);
        }
Example #13
0
        public void FracConstructor()
        {
            BigFraction a        = new BigFraction(new BigInteger(3300), new BigInteger(9900));
            BigFraction expected = new BigFraction(new BigInteger(33), new BigInteger(99));

            Assert.Equal(expected, a);
        }
Example #14
0
        public void gethashcode()
        {
            BigFraction a = new BigFraction(new BigInteger(1000), new BigInteger(100));
            BigFraction b = new BigFraction(new BigInteger(100), new BigInteger(10));

            Assert.Equal(a.GetHashCode(), b.GetHashCode());
        }
Example #15
0
        public void Case9()
        {
            BigFraction bigA = new BigFraction(new BigInteger(decimal.MaxValue), new BigInteger(decimal.MinValue));
            var         a    = bigA.ToDecimal();

            Assert.Equal(-1m, a);
        }
Example #16
0
        public void BigIntConstructor()
        {
            BigFraction a        = new BigFraction(new BigInteger(33));
            BigFraction expected = new BigFraction(new BigInteger(33), new BigInteger(1));

            Assert.Equal(expected, a);
        }
Example #17
0
        public void Case5()
        {
            BigFraction bigA = new BigFraction(new BigInteger(decimal.MinValue) * 10, new BigInteger(decimal.MaxValue) * 20);
            var         a    = bigA.ToDecimal();

            Assert.Equal(-0.5m, a);
        }
Example #18
0
        public void Case6()
        {
            BigFraction bigA = new BigFraction(new BigInteger(decimal.MinValue) * 10, new BigInteger(decimal.MinValue) / 2);
            var         a    = bigA.ToDecimal();

            Assert.Equal(20m, a);
        }
Example #19
0
        public void DecConstructor()
        {
            BigFraction a        = new BigFraction(new decimal(8984.65));
            BigFraction expected = new BigFraction(new BigInteger(898465), new BigInteger(100));

            Assert.Equal(expected, a);
        }
Example #20
0
        public void Case3()
        {
            BigFraction bigA = new BigFraction(new BigInteger(decimal.MaxValue) * 100, new BigInteger(decimal.MaxValue) * 10);
            var         a    = bigA.ToDecimal();

            Assert.Equal(10m, a);
        }
Example #21
0
        public void SmallerEqual()
        {
            BigFraction a = new BigFraction(-251.15);

            Assert.True(a <= 4.2);
            Assert.True(a <= -251.15);
        }
Example #22
0
        public void GreaterEqual()
        {
            BigFraction a = new BigFraction(251.15);

            Assert.True(a >= 251.15);
            Assert.True(a >= 4.2);
        }
Example #23
0
        public void DoubletoBigFrac()
        {
            double      a        = 1000.01;
            BigFraction afrac    = a;
            BigFraction expected = new BigFraction(new BigInteger(100001), new BigInteger(100));

            Assert.Equal(expected, afrac);
        }
Example #24
0
        public void doubletofraction()
        {
            double      a        = 0.5;
            BigFraction b        = BigFraction.FromDouble(a, 1e-15);
            BigFraction expected = new BigFraction(new BigInteger(1), new BigInteger(2));

            Assert.Equal(expected, b);
        }
Example #25
0
        public void Mulplication()
        {
            BigFraction a        = new BigFraction(-5.25);
            BigFraction b        = new BigFraction(10.1);
            BigFraction expected = new BigFraction(-53.025);

            Assert.Equal(expected, a * b);
        }
Example #26
0
        public void Modulus()
        {
            BigFraction a        = new BigFraction(5.25);
            BigInteger  b        = new BigInteger(5);
            BigFraction expected = new BigFraction(0.25);

            Assert.Equal(expected, a % b);
        }
Example #27
0
        public void BigInttoBigFrac()
        {
            BigInteger  a        = new BigInteger(1000);
            BigFraction afrac    = a;
            BigFraction expected = new BigFraction(new BigInteger(1000), new BigInteger(1));

            Assert.Equal(expected, afrac);
        }
Example #28
0
        public void Modulus()
        {
            BigFraction a        = new BigFraction(5.25);
            BigFraction c        = a % 5;
            BigFraction expected = new BigFraction(0.25);

            Assert.Equal(expected, c);
        }
Example #29
0
        public void DectoBigFrac()
        {
            Decimal     a        = new Decimal(1000.01);
            BigFraction afrac    = a;
            BigFraction expected = new BigFraction(new BigInteger(100001), new BigInteger(100));

            Assert.Equal(expected, afrac);
        }
Example #30
0
        public void Division()
        {
            BigFraction a        = new BigFraction(5.25);
            BigFraction b        = new BigFraction(1.25);
            BigFraction expected = new BigFraction(4.20);

            Assert.Equal(expected, a / b);
        }
Example #31
0
        public void tobiginteger()
        {
            BigFraction a        = new BigFraction(new BigInteger(1000), new BigInteger(100));
            BigInteger  aint     = a.ToBigInteger();
            BigInteger  expected = new BigInteger(10);

            Assert.Equal(expected, aint);
        }
Example #32
0
        public void TestRef()
        {
            BigFraction[] testArray = new BigFraction[3];
            BigFraction init = new BigFraction(-1, 1);

            testArray[0] = init;
            testArray[1] = init;

            Assert.IsFalse(ReferenceEquals(testArray[0], testArray[1]));
        }
        public static BigContinuedFraction FromBigFraction(BigFraction fraction)
        {
            if (fraction.Denominator.IsZero)
            {
                throw new ArithmeticException("Cannot generate continued fraction from NaN, Infinity, or -Infinity");
            }

            bool first = true;
            BigInteger initial = BigInteger.Zero;
            List<BigInteger> quotients = new List<BigInteger>();
            while (true)
            {
                BigFraction remainder;
                BigInteger floor = BigFraction.DivRem(fraction, BigFraction.One, out remainder).Numerator;

                if (first)
                {
                    first = false;
                    initial = floor;
                }
                else
                {
                    quotients.Add(floor);
                }

                if (remainder.IsZero)
                {
                    return new BigContinuedFraction(initial, quotients.ToArray());
                }

                fraction = BigFraction.Reciprocal(remainder);
            }
        }
Example #34
0
 	/*
 	Tests manually that should their be a prohibited interval, that
 	the difference between 2 values in the sequence is > a target difference.
 	*/
 	private void testForbiddenIntervalHelper(int height, int deltaP, 
 		int target, BigFraction lower, BigFraction upper, bool found)
 	{
 		for(int p0 = 0; p0 <= 3 * height; ++p0)
 		{
 			int p0_adj = (int) PongMain.calcAdd(p0, deltaP, 0, height);
 			int p1 = (int) PongMain.calcAdd(p0, deltaP, 1, height);
 			
 			int diff = Math.Abs(p0_adj-p1);
 			
 			Logger.LogTrace("testForbiddenIntervalHelper p0 [{}] p1 [{}] diff [{}] target {} lower {} upper {}",
 				p0, p1, diff, target, lower, upper);
 			
 			int p0_mh = p0 % height;
 			
 			if (!found)
 			{
 				Assert.IsTrue(diff <= target);
 				
 			} else if (p0_mh < lower) 
 			{
 				Assert.IsTrue(diff > target);	
 			} else if (p0_mh > upper) {
 				Assert.IsTrue(diff > target);	
 			} else {
 				Assert.IsTrue(diff <= target);	
 			}
 			
 		}
 		
 	}