Example #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));
            }
        }
Example #2
0
        public override async Task <bool> TestAsync(BigInteger source)
        {
            var trivialCheck = this.CheckEdgeCases(source);

            if (trivialCheck.HasValue)
            {
                return(trivialCheck.Value);
            }

            var sourceSquareRoot = BigMath.SquareRoot(source);

            var isPrime = true;

            await Task.Run(() =>
            {
                for (var i = BigIntegerHelpers.Two; i <= sourceSquareRoot; i = BigInteger.Add(i, BigInteger.One))
                {
                    var rem = BigInteger.Remainder(source, i);

                    if (rem != BigInteger.Zero)
                    {
                        continue;
                    }

                    isPrime = false;
                    break;
                }
            });

            return(isPrime);
        }
Example #3
0
 public DoublevsBigDouble()
 {
     first        = BigMath.RandomBigDouble(100);
     second       = BigMath.RandomBigDouble(100);
     firstDouble  = first.ToDouble();
     secondDouble = second.ToDouble();
 }
Example #4
0
        public void AssertFactorialOf15()
        {
            string val1    = "15";
            string results = BigMath.Factorial(val1);

            Assert.AreEqual("1307674368000", results);
        }
Example #5
0
        public void AssertFactorial()
        {
            string val1    = "2";
            string results = BigMath.Factorial(val1);

            Assert.AreEqual("2", results);
        }
Example #6
0
        public bool Contains(Point2BR point)
        {
            if (point.X > BigMath.Max(this.point2BR_0.X, this.point2BR_1.X) || point.X < BigMath.Min(this.point2BR_0.X, this.point2BR_1.X) || (point.Y > BigMath.Max(this.point2BR_0.Y, this.point2BR_1.Y) || point.Y < BigMath.Min(this.point2BR_0.Y, this.point2BR_1.Y)))
            {
                return(false);
            }
            if (this.point2BR_0 == this.point2BR_1)
            {
                return(true);
            }
            Vector2BR delta = this.GetDelta();
            Vector2BR u     = point - this.point2BR_0;
            Vector2BR v     = new Vector2BR(-delta.Y, delta.X);

            if (!Vector2BR.DotProduct(u, v).IsZero)
            {
                return(false);
            }
            BigRational bigRational = Vector2BR.DotProduct(u, delta);

            if (bigRational.IsNegative)
            {
                return(false);
            }
            BigRational lengthSquared = delta.GetLengthSquared();

            return(!(bigRational > lengthSquared));
        }
Example #7
0
        static void Main(string[] args)
        {
            /*var random = new Random();
             *
             *
             *
             * for (int i = 0; i < 5; i++)
             * {
             *  Console.WriteLine(random.Next()); // 0 - 2^32
             *  Console.WriteLine(random.Next(100)); // 0 - 100
             *  Console.WriteLine(random.Next(10, 20)); // 10 - 20
             *
             *  Console.WriteLine(random.NextDouble()); // 0.0 - 1.0
             *
             *  var buffer = new byte[10];
             *  random.NextBytes(buffer);
             *  Console.WriteLine(BitConverter.ToString(buffer));
             *
             *
             *
             *  var karakteri = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
             *  string randomText = "";
             *  for (int a = 0; a < 35; a++)
             *  {
             *      Console.WriteLine(a);
             *      randomText += karakteri[random.Next(karakteri.Length - 1)]; // lenght = 3 || index {0 , 1 , 2}
             *  }
             *  Console.WriteLine(randomText);
             *
             *
             *  Console.WriteLine();
             * }*/

            Console.WriteLine(BigMath.GetPi(500, 100000));
        }
Example #8
0
        /// <summary>
        /// Create a permutation using an encoded permutation
        /// </summary>
        ///
        /// <param name="Encoded">The encoded permutation</param>
        public Permutation(byte[] Encoded)
        {
            if (Encoded.Length <= 4)
            {
                throw new ArgumentException("Permutation: Invalid encoding!");
            }

            int n    = LittleEndian.OctetsToInt(Encoded, 0);
            int size = BigMath.CeilLog256(n - 1);

            if (Encoded.Length != 4 + n * size)
            {
                throw new ArgumentException("Permutation: Invalid encoding!");
            }

            _perm = new int[n];
            for (int i = 0; i < n; i++)
            {
                _perm[i] = LittleEndian.OctetsToInt(Encoded, 4 + i * size, size);
            }

            if (!IsPermutation(_perm))
            {
                throw new ArgumentException("Permutation: Invalid encoding!");
            }
        }
Example #9
0
        void ISqlFormattable.AppendTo(SqlStringBuilder builder)
        {
            switch (state)
            {
            case (NumericState.None): {
                if (CanBeInt32 || CanBeInt64)
                {
                    builder.Append(valueAsLong);
                }
                else
                {
                    var norm = BigMath.StripTrailingZeros(innerValue);
                    builder.Append(norm.ToString());
                }
                break;
            }

            case (NumericState.NegativeInfinity):
                builder.Append("-Infinity");
                break;

            case (NumericState.PositiveInfinity):
                builder.Append("+Infinity");
                break;

            case (NumericState.NotANumber):
                builder.Append("NaN");
                break;

            default:
                throw new InvalidCastException("Unknown number state");
            }
        }
Example #10
0
        public void AssertFactorialOf10()
        {
            string val1    = "10";
            string results = BigMath.Factorial(val1);

            Assert.AreEqual("3628800", results);
        }
Example #11
0
        public void AssertSubtractMethodLargeNumberToOne()
        {
            string val1    = "987654321987654321";
            string val2    = "987654321987654320";
            string results = BigMath.Subtract(val1, val2);

            Assert.AreEqual("1", results);
        }
Example #12
0
        public void AssertMultiplyMethod2Times2()
        {
            string val1    = "2";
            string val2    = "2";
            string results = BigMath.Multiply(val1, val2);

            Assert.AreEqual("4", results);
        }
Example #13
0
        public void AssertMultiplyMethod123456789Times123456789()
        {
            string val1    = "123456789";
            string val2    = "123456789";
            string results = BigMath.Multiply(val1, val2);

            Assert.AreEqual("15241578750190521", results);
        }
Example #14
0
        public void AssertAddMethod()
        {
            string val1    = "1";
            string val2    = "1";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("2", results);
        }
Example #15
0
        public void AssertAddMethodWithNumberGreaterThanUi64()
        {
            string val1    = "18446744073709551615";
            string val2    = "11111111111111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("29557855184820662726", results);
        }
Example #16
0
        public void AssertAddMethodWithNumberGreaterThani64()
        {
            string val1    = "9223372036854775807";
            string val2    = "1111111111111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("10334483147965886918", results);
        }
Example #17
0
        public void AssertAddMethodWithNumberGreaterThanUint()
        {
            string val1    = "4294967295";
            string val2    = "1111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("5406078406", results);
        }
Example #18
0
        public void AssertAddMethodWithNumberGreaterThanInt()
        {
            string val1    = "2147483647"; //int max
            string val2    = "1111111111";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("3258594758", results);
        }
Example #19
0
        public void AssertAddMethod100Plus100()
        {
            string val1    = "100";
            string val2    = "100";
            string results = BigMath.Add(val1, val2);

            Assert.AreEqual("200", results);
        }
Example #20
0
 public void Setup()
 {
     firstBigDouble  = BigMath.RandomBigDouble(100);
     firstQuad       = new Quad(firstBigDouble.ToDouble());
     secondBigDouble = BigMath.RandomBigDouble(100);
     secondQuad      = new Quad(secondBigDouble.ToDouble());
     smallDouble     = BigMath.RandomBigDouble(2).ToDouble();
 }
Example #21
0
        public void AssertSubtractMethod10Minus1()
        {
            string val1    = "10";
            string val2    = "1";
            string results = BigMath.Subtract(val1, val2);

            Assert.AreEqual("9", results);
        }
Example #22
0
        public void AssertExponential()
        {
            string val1    = "10";
            string val2    = "2";
            string results = BigMath.Exponential(val1, val2);

            Assert.AreEqual("100", results);
        }
Example #23
0
        public void AssertSubtractMethodLargeNumberToZero()
        {
            string val1    = "123456789";
            string val2    = "123456789";
            string results = BigMath.Subtract(val1, val2);

            Assert.AreEqual("0", results);
        }
Example #24
0
        public void AssertExponentialOf15To5()
        {
            string val1    = "15";
            string val2    = "5";
            string results = BigMath.Exponential(val1, val2);

            Assert.AreEqual("759375", results);
        }
Example #25
0
        public void AssertExponentialOf20To20()
        {
            string val1    = "20";
            string val2    = "20";
            string results = BigMath.Exponential(val1, val2);

            Assert.AreEqual("104857600000000000000000000", results);
        }
Example #26
0
        public void AssertExponentialOf11To2()
        {
            string val1    = "11";
            string val2    = "2";
            string results = BigMath.Exponential(val1, val2);

            Assert.AreEqual("121", results);
        }
Example #27
0
        public SqlNumber Root(int n)
        {
            if (State == NumericState.None)
            {
                return(new SqlNumber(BigMath.Root(n, innerValue)));
            }

            return(this);
        }
Example #28
0
        public SqlNumber Pow(SqlNumber exp)
        {
            if (State == NumericState.None)
            {
                return(new SqlNumber(BigMath.Pow(innerValue, exp.innerValue)));
            }

            return(this);
        }
Example #29
0
        public SqlNumber Log2()
        {
            if (State == NumericState.None)
            {
                return(new SqlNumber(BigMath.Log(innerValue)));
            }

            return(this);
        }
Example #30
0
        public static SqlNumber Round(SqlNumber value, int precision)
        {
            if (SqlNumber.IsNumber(value))
            {
                var result = BigMath.Round(value.innerValue, new MathContext(precision, RoundingMode.HalfUp));
                return(new SqlNumber(result));
            }

            return(value);
        }