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 AssertSubtractMethod10Minus1()
        {
            string val1    = "10";
            string val2    = "1";
            string results = BigMath.Subtract(val1, val2);

            Assert.AreEqual("9", results);
        }
Exemplo n.º 3
0
        public void AssertSubtractMethodLargeNumberToOne()
        {
            string val1    = "987654321987654321";
            string val2    = "987654321987654320";
            string results = BigMath.Subtract(val1, val2);

            Assert.AreEqual("1", results);
        }
Exemplo n.º 4
0
        public void AssertSubtractMethodLargeNumberToZero()
        {
            string val1    = "123456789";
            string val2    = "123456789";
            string results = BigMath.Subtract(val1, val2);

            Assert.AreEqual("0", results);
        }
Exemplo n.º 5
0
        public void SubtractionEqualsTest()
        {
            var a = ASCIIEncoding.Default.GetBytes("1000000");
            var b = ASCIIEncoding.Default.GetBytes("1000000");

            bool borrow;
            var  result = BigMath.Subtract(a, b, false, out borrow);

            Assert.AreEqual("0000000", ASCIIEncoding.Default.GetString(result));
        }
Exemplo n.º 6
0
        public static SqlNumber Subtract(SqlNumber a, SqlNumber b, int precision)
        {
            if (SqlNumber.IsNumber(a))
            {
                if (SqlNumber.IsNumber(b))
                {
                    var context = new MathContext(precision);
                    var result  = BigMath.Subtract(a.innerValue, b.innerValue, context);

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

                return(new SqlNumber(b.InverseState(), null));
            }

            return(a);
        }
Exemplo n.º 7
0
        public void SubtractionShallHaventBorrowTest()
        {
            var a = ASCIIEncoding.Default.GetBytes("1001");
            var b = ASCIIEncoding.Default.GetBytes("1000");

            bool borrow;
            var  result = BigMath.Subtract(a, b, false, out borrow);

            Assert.AreEqual(false, borrow);

            a = ASCIIEncoding.Default.GetBytes("10");
            b = ASCIIEncoding.Default.GetBytes("07");

            result = BigMath.Subtract(a, b, borrow, out borrow).Concat(result).ToArray();

            Assert.AreEqual("030001", ASCIIEncoding.Default.GetString(result));
        }
Exemplo n.º 8
0
        public void SubtractionRandomNumbersTest()
        {
            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);

                if (b > a)
                {
                    var tmp = a;
                    b = a;
                    a = tmp;
                }

                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);

                bool hasBorrow = false;
                var  result    = BigMath.Subtract(aBytes, bBytes, false, out hasBorrow);

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

                Assert.AreEqual((long)a - (long)b, long.Parse(number));
            }
        }
Exemplo n.º 9
0
 public void AssertStaticSubtractMethod()
 {
     var testLeft   = "1";
     var testRight  = "1";
     var testResult = BigMath.Subtract(testLeft, testRight);
 }