Exemplo n.º 1
0
        private Func <IEnumerable <byte>, IEnumerable <byte>, byte[]> PrepareOperationFunction(Lazy <bool?> firstIsBigger)
        {
            //If we have subtraction, change second number sign
            if (Operation == BigNumber.Operation.Substraction)
            {
                m_Reader2.Info.IsNegative = !m_Reader2.Info.IsNegative;
            }

            //add numbers if they have the same sign
            if (m_Reader1.Info.IsNegative == m_Reader2.Info.IsNegative)
            {
                m_ResultIsNegative = m_Reader1.Info.IsNegative;
                return((f1, f2) => BigMath.Add(
                           f1, f2, m_PreviousHasCarry, out m_PreviousHasCarry));
            }

            if (!firstIsBigger.Value.HasValue)
            {
                return(null);
            }

            //we must use subtract
            if (firstIsBigger.Value.Value)
            {
                m_ResultIsNegative = m_Reader1.Info.IsNegative;
                return((f1, f2) =>
                       BigMath.Subtract(f1, f2, m_PreviousHasBorrow, out m_PreviousHasBorrow));
            }
            else
            {
                m_ResultIsNegative = m_Reader2.Info.IsNegative;
                return((f1, f2) =>
                       BigMath.Subtract(f2, f1, m_PreviousHasBorrow, out m_PreviousHasBorrow));
            }
        }
Exemplo n.º 2
0
        public void AssertAddMethodWithNumberGreaterThanUi64()
        {
            string val1    = "18446744073709551615";
            string val2    = "11111111111111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("29557855184820662726", results);
        }
Exemplo n.º 3
0
        public void AssertAddMethodWithNumberGreaterThani64()
        {
            string val1    = "9223372036854775807";
            string val2    = "1111111111111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("10334483147965886918", results);
        }
Exemplo n.º 4
0
        public void AssertAddMethodWithNumberGreaterThanUint()
        {
            string val1    = "4294967295";
            string val2    = "1111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("5406078406", results);
        }
Exemplo n.º 5
0
        public void AssertAddMethodWithNumberGreaterThanInt()
        {
            string val1    = "2147483647"; //int max
            string val2    = "1111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("3258594758", results);
        }
Exemplo n.º 6
0
        public void AssertAddMethod100Plus100()
        {
            string val1    = "100";
            string val2    = "100";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("200", results);
        }
Exemplo n.º 7
0
        public void AssertAddMethod()
        {
            string val1    = "1";
            string val2    = "1";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("2", results);
        }
Exemplo n.º 8
0
        public void AdditionHasCarryTest()
        {
            string a        = "999999999999999999999999999999999999";
            string b        = "1";
            string expected = "000000000000000000000000000000000001";

            bool hasCarry;

            byte[] aBytes = Encoding.ASCII.GetBytes(a);
            byte[] bBytes = Encoding.ASCII.GetBytes(b.PadLeft(a.Length, '0'));

            var result = BigMath.Add(aBytes, bBytes, true, out hasCarry);

            Assert.AreEqual(expected, Encoding.ASCII.GetString(result));
            Assert.IsTrue(hasCarry, "Must have carry");
        }
Exemplo n.º 9
0
        public void AdditionTest()
        {
            string a        = "1000";
            string b        = "44";
            string expected = "1044";

            bool hasCarry;

            byte[] aBytes = Encoding.ASCII.GetBytes(a);
            byte[] bBytes = Encoding.ASCII.GetBytes(b.PadLeft(a.Length, '0'));

            var result = BigMath.Add(aBytes, bBytes, false, out hasCarry);

            Assert.AreEqual(expected, Encoding.ASCII.GetString(result));
            Assert.IsFalse(hasCarry, "Must not have carry");
        }
Exemplo n.º 10
0
        public static SqlNumber Add(SqlNumber a, SqlNumber b, int precision)
        {
            if (SqlNumber.IsNumber(a))
            {
                if (SqlNumber.IsNumber(b))
                {
                    var context = new MathContext(precision);
                    var result  = BigMath.Add(a.innerValue, b.innerValue, context);

                    return(new SqlNumber(SqlNumber.NumericState.None, result));
                }

                return(b);
            }

            return(a);
        }
Exemplo n.º 11
0
        public void AdditionRandomNumbersTest()
        {
            var rand  = new Random();
            var rand2 = new Random();

            for (int i = 0; i < 10; i++)
            {
                var a = rand.Next(0, Int32.MaxValue);
                var b = rand.Next(0, Int32.MaxValue);

                bool hasCarry = false;

                string aString = a.ToString();
                string bString = b.ToString();

                if (aString.Length != bString.Length)
                {
                    int length = Math.Max(aString.Length, bString.Length);
                    aString = aString.PadLeft(length, '0');
                    bString = bString.PadLeft(length, '0');
                }

                byte[] aBytes = Encoding.ASCII.GetBytes(aString);
                byte[] bBytes = Encoding.ASCII.GetBytes(bString);

                var result = BigMath.Add(aBytes, bBytes, false, out hasCarry);

                string number = Encoding.ASCII.GetString(result);

                if (hasCarry)
                {
                    number = "1" + number;
                }

                Assert.AreEqual((long)a + (long)b, long.Parse(number));
            }
        }
Exemplo n.º 12
0
 public void AssertStaticAddMethod()
 {
     var testLeft   = "1";
     var testRight  = "1";
     var testResult = BigMath.Add(testLeft, testRight);
 }