Пример #1
0
        public static void GenerateServerK(BigNum a, BigNum b, BigNum k)
        {
            var prime = new BigNum();

            prime.ReadBigEndian(ConstantPrime, 0, ConstantPrime.Length);

            k.ModExp(b, a, prime);
        }
Пример #2
0
        public void Sub()
        {
            BigNum num1 = new BigNum(1000000000);
            BigNum num2 = new BigNum(200000000000);
            BigNum num3 = new BigNum(1000000000 - 200000000000);

            Assert.AreEqual(num1 - num2, num3);
        }
Пример #3
0
    public static BigNum suma(BigNum A, BigNum B)
    {
        BigNum res = new BigNum(0);

        res.przypisz(A);
        res.plus(B);
        return(res);
    }
Пример #4
0
    public static BigNum iloraz(BigNum A, BigNum B)
    {
        BigNum res = new BigNum(0);

        res.przypisz(A);
        res.podziel(B);
        return(res);
    }
Пример #5
0
    /// <summary>
    /// Consume as much of this unit as possible, up to amount.
    /// </summary>
    /// <param name="type"></param>
    /// <param name="amount"></param>
    /// <returns></returns>
    public BigNum ConsumeUpTo(Units type, BigNum amount)
    {
        BigNum amountConsumed = Mathf.Min(Resources[type].Amount, amount);

        Resources[type].Amount -= amountConsumed;

        return(amountConsumed);
    }
Пример #6
0
    public static BigNum iloczyn(BigNum A, BigNum B)
    {
        BigNum res = new BigNum(0);

        res.przypisz(A);
        res.razy(B);
        return(res);
    }
Пример #7
0
		public static BigNum Pow(BigNum num, BigNum exponent) {
			
			// http://en.wikipedia.org/wiki/Exponentiation
			
			// a^x == E^( x * ln(a) )
			
			return Exp( exponent * Log( num ) );
		}
Пример #8
0
 public void SingleParamConstructorWithValidString()
 {
     foreach (string s in _validConstructorStrings)
     {
         BigNum num = new BigNum(s);
         Assert.NotNull(num);
     }
 }
Пример #9
0
        public void RemoveTralingDecimalPoint()
        {
            string s = "110.";

            s = BigNum.RemoveTrailingZeros(s);
            Console.WriteLine("s: " + s);
            Assert.AreEqual("110", s);
        }
Пример #10
0
    public static BigNum roznica(BigNum A, BigNum B)
    {
        BigNum res = new BigNum(0);

        res.przypisz(A);
        res.minus(B);
        return(res);
    }
Пример #11
0
    private void SetResource(Units type, BigNum value)
    {
        Resources[type].Amount = value;

        if (Resources[type].MaxValue > 0)
        {
            Resources[type].Amount = Mathf.Min(Resources[type].Amount, Resources[type].MaxValue);
        }
    }
Пример #12
0
        public void TestMultiplication(string hex1, string hex2, string h1_mul_h2)
        {
            var    a          = new BigNum(hex1);
            var    b          = new BigNum(hex2);
            var    calculator = new Operation();
            BigNum result     = calculator.Multiplication(a, b);

            Assert.AreEqual(h1_mul_h2, result.ToString());
        }
Пример #13
0
        public void InitializeWithNegativeValue()
        {
            BigNum num = new BigNum();

            num.Initialize("-100");
            Assert.IsTrue(num.Mantissa.Equals(100));
            Assert.AreEqual(0, num.Exponent);
            Assert.AreEqual(true, num.Sign);
        }
Пример #14
0
        public void PruneLeadingZeros()
        {
            string pruned = BigNum.PruneLeadingZeros("0.07");

            Assert.AreEqual(".07", pruned);

            pruned = BigNum.PruneLeadingZeros("000000010.07");
            Assert.AreEqual("10.07", pruned);
        }
Пример #15
0
        public void GreaterThanWithSecondParamNegative()
        {
            BigNum num1 = new BigNum("1");
            BigNum num2 = new BigNum("-1");

            bool result = (num1 > num2);

            Assert.IsTrue(result);
        }
Пример #16
0
        public void GreaterThanWithFirstParamNegative()
        {
            BigNum num1 = new BigNum("-1");
            BigNum num2 = new BigNum("1");

            bool result = (num1 > num2);

            Assert.IsFalse(result);
        }
Пример #17
0
        public void GreaterThanWithEqualPositiveDecimals()
        {
            BigNum num1 = new BigNum("1.2");
            BigNum num2 = new BigNum("1.4");

            bool result = (num1 > num2);

            Assert.IsFalse(result);
        }
Пример #18
0
        public void DivisionWith()
        {
            BigNum num1 = new BigNum("27");
            BigNum num2 = new BigNum("7");

            BigNum sum = num1 / num2;

            Assert.NotNull(sum);
        }
Пример #19
0
        public void InitializeWithValueMuchLessThanOne()
        {
            BigNum num = new BigNum();

            num.Initialize(".000000001");
            Assert.IsTrue(num.Mantissa.Equals(1));
            Assert.AreEqual(-9, num.Exponent);
            Assert.AreEqual(false, num.Sign);
        }
Пример #20
0
        public void BvExtractExpr()
        {
            var bv2_8 = new LiteralExpr(Token.NoToken, BigNum.FromInt(2), 8);
            var A     = new BvExtractExpr(Token.NoToken, bv2_8, 6, 0);
            var B     = d.Visit(A);

            // The duplicator should ensure we get new BVExtractExprs
            Assert.AreNotSame(A, B);
        }
Пример #21
0
        public void NaryExpr()
        {
            var bv1_8 = new LiteralExpr(Token.NoToken, BigNum.FromInt(1), 8);
            var bv2_8 = new LiteralExpr(Token.NoToken, BigNum.FromInt(2), 8);
            var A     = NAryExpr.Eq(bv1_8, bv2_8);
            var B     = d.Visit(A);

            Assert.AreNotSame(A, B);
        }
Пример #22
0
        public void TestShiftBitsToHigh(string hex1, int shift, string hex2)
        {
            var       a          = new BigNum(hex1);
            var       b          = new BigNum(hex2);
            Operation calculator = new Operation();
            var       c          = calculator.ShiftBitsToHigh(a, shift);

            Assert.AreEqual(hex2, c.ToString());
        }
Пример #23
0
        public void DivideSon()
        {
            BigNum myBigNum1 = new BigNum("1");
            BigNum myBigNum2 = new BigNum("3");

            BigNum quotient = myBigNum1 / myBigNum2;

            int x = 0;
        }
Пример #24
0
        public void TestCompare(string hex1, string hex2, int num)
        {
            var a          = new BigNum(hex1);
            var b          = new BigNum(hex2);
            var calculator = new Operation();
            var result     = calculator.Compare(a, b);

            Assert.AreEqual(num, result);
        }
Пример #25
0
        public void PruneTrailingZeros()
        {
            string pruned = BigNum.PruneTrailingZeros("0.789000001");

            Assert.AreEqual("0.789000001", pruned);

            pruned = BigNum.PruneTrailingZeros("0.78900000");
            Assert.AreEqual("0.789", pruned);
        }
Пример #26
0
        public void TestSubstraction(string hex1, string hex2, string h1_minus_h2)
        {
            var a          = new BigNum(hex1);
            var b          = new BigNum(hex2);
            var calculator = new Operation();
            var result     = calculator.Substraction(a, b);

            Assert.AreEqual(h1_minus_h2, result.ToString());
        }
Пример #27
0
        public void TestGorner(string hex1, string hex2, string hex3)
        {
            var       a          = new BigNum(hex1);
            var       b          = new BigNum(hex2);
            Operation calculator = new Operation();
            var       c          = calculator.LongPowerWindow(a, b);

            Assert.AreEqual(hex3, c.ToString());
        }
Пример #28
0
        public void GetRightHandSideOfNegativeNumberWithDecimal()
        {
            BigNum     num      = new BigNum("-12.34");
            BigInteger expected = BigInteger.Parse("-34");

            BigInteger actual = BigNum.GetRightOfDecimal(num);

            Assert.AreEqual(expected, actual);
        }
Пример #29
0
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            BigNum num1 = new BigNum(this.txtNum1.Text);
            BigNum num2 = new BigNum(this.txtNum2.Text);

            BigNum sum = num1.Add(num2);

            this.lblResult.Content = sum.Value();
        }
Пример #30
0
        public void Mul()
        {
            BigNum num1 = new BigNum(10000000000000);
            BigNum num2 = new BigNum(20000000000000);
            BigNum num3 = new BigNum(-500);

            Assert.AreEqual(num1 * num2, num2 * num1);
            Assert.AreEqual(num1 * num3, num3 * num1);
        }
Пример #31
0
 void przypisz(BigNum A)
 {
     this.znak = A.znak;
     this.pos  = A.pos;
     for (int i = 0; i < max_size; i++)
     {
         this.cyfry[i] = A.cyfry[i];
     }
 }
Пример #32
0
        public void quotent()
        {
            BigNum myBigNum = new BigNum("3.1415");
            BigNum bn       = new BigNum("3.004837");
            BigNum quotient = myBigNum / bn;
            string s        = quotient.ToString();

            Assert.AreEqual(s, "1.0454810027964911241441715474");
        }
Пример #33
0
        public override bool Equals(BigNum other)
        {
            //			if( !other.Floor().Equals( other ) ) { // check it's an integer
            //
            //				return false;
            //			}

            BigInt o = E( other );

            return _v.Equals( o._v );
        }
Пример #34
0
		/// <summary>Computes Euler's number raised to the specified exponent</summary>
		public static BigNum Exp(BigNum exponent) {
			
			const int iterations = 25;
			
			// E^x ~= sum(int i = 0 to inf, x^i/i!)
			//     ~= 1 + x + x^2/2! + x^3/3! + etc
			
			BigNum ret = exponent.Factory.Unity;
			ret += exponent;
			
			for(int i=2;i<iterations;i++) {
				
				BigNum numerator = exponent.Power( i );
				BigNum denominat = exponent.Factory.Create( Factorial( i ) );
				
				BigNum addThis = numerator / denominat;
				
				ret += addThis;
			}
			
			return ret;
		}
Пример #35
0
		public static BigNum Cos(BigNum theta) {
			
			// using Cos(x) == Sin(x + 90deg)
			
			theta += theta.Factory.HalfPi;
			
			return Sin( theta );
		}
Пример #36
0
		public static BigNum Pow(BigNum num, Int32 exponent) {
			
			return num.Power( exponent );
		}
Пример #37
0
 public Variable(String name, BigNum value)
 {
     Name  = name;
     Value = value;
 }
Пример #38
0
		public static BigNum Min(BigNum x, BigNum y) {
			
			return x > y ? y : x;
		}
Пример #39
0
		public static Boolean IsInteger(BigNum x) {
			
			return x.Floor() == x;
		}
Пример #40
0
 private DNumber(BigNum num)
 {
     _value = num;
 }
Пример #41
0
		/// <summary>Computes the Natural Logarithm (Log to base E, or Ln(x)) of the specified argument</summary>
		public static BigNum Log(BigNum x) {
			
			BigNumFactory f = x.Factory;
			
			if( x <= f.Zero ) throw new ArgumentOutOfRangeException("x", "Must be a positive real number");
			
			const int iterations = 25;
			
			BigNum one = f.Unity;
			BigNum two = f.Create(2);
			
			// ln(z) == 2 * Sum(n=0;n<inf;n++) { (1 / (2n+1) ) * Pow( (z-1)/(z+1), 2n+1) }
			
			BigNum ret = f.Zero;
			
			for(int n=0;n<iterations;n++) {
				
				int denom = 2*n + 1;
				
				Double coefficient = 1 / denom;
				BigNum otherHalf   = Pow( ( x - one ) / ( x + one ), denom ); // hurrah for integer powers
				
				BigNum sumThis = f.Create( coefficient ) * otherHalf;
				ret += sumThis;
			}
			
			return ret;
		}
Пример #42
0
		/// <summary>Computes the Logarithm of x for base b. So log10(100) is Log(10,100)</summary>
		public static BigNum Log(BigNum x, BigNum b) {
			
			return Log( x ) / Log( b );
		}
Пример #43
0
 public DNumber(long value)
 {
     _value = BigFloatFactory.Instance.Create(value);
 }
Пример #44
0
 public override BigNum Create(BigNum value)
 {
     throw new NotImplementedException();
 }
Пример #45
0
        /////////////////////////////////////////
        public virtual Boolean TryParse(String value, out BigNum number)
        {
            try {

                number = Create( value );

            } catch(FormatException) {

                number = null;
                return false;
            }

            return true;
        }
Пример #46
0
		public static BigNum Ceiling(BigNum num) {
			
			return num.Ceiling();
		}
Пример #47
0
		public static BigNum Sin(BigNum theta) {
			
			BigNumFactory f = theta.Factory;
			
			// calculate sine using the taylor series, the infinite sum of x^r/r! but to n iterations
			BigNum retVal = f.Zero;
			
			// first, reduce this to between 0 and 2Pi
			if( theta > f.TwoPi || theta < f.Zero )
				theta = theta % f.TwoPi;
			
			Boolean subtract = false;
			
			// using bignums for sine computation is too heavy. It's faster (and just as accurate) to use Doubles
	#if DoubleTrig
			
			Double thetaDbl = Double.Parse( theta.ToString(), Cult.InvariantCulture );
			for(Int32 r=0;r<20;r++) { // 20 iterations is enough, any more just yields inaccurate less-significant digits
				
				Double xPowerR = Math.Pow( thetaDbl, 2*r + 1 );
				Double factori = BigMath.Factorial( (double)( 2*r + 1 ) );
				
				Double element = xPowerR / factori;
				
				Double addThis = subtract ? -element : element;
				
				BigNum addThisBig = f.Create( addThis );
				
				retVal += addThisBig;
				
				subtract = !subtract;
			}
			
			
	#else
			
			for(Int32 r=0;r<_iterations;r++) {
				
				BigNum xPowerR = theta.Power(2*r +1);
				BigNum factori = Factorial( 2*r + 1);
				
				BigNum element = xPowerR / factori;
				
				retVal += subtract ? -element : element;
				
				subtract = !subtract;
			}
			
	#endif
			
			// TODO: This calculation generates useless and inaccurate trailing digits that must be truncated
			// so truncate them, when I figure out how many digits can be removed
			
			retVal.Truncate( 10 );
			
			return retVal;
			
		}
Пример #48
0
		public static BigNum Floor(BigNum num) {
			
			return num.Floor();
		}
Пример #49
0
		public static BigNum Abs(BigNum num) {
			
			return num.Absolute();
		}
Пример #50
0
		/////////////////////////////////////////
		
		public static BigNum Gamma(BigNum z) {
			
			// http://www.rskey.org/gamma.htm
			// n! = nn√2πn exp(1/[12n + 2/(5n + 53/42n)] – n)(1 + O(n–8))
			// which requires the exponential function, and some function O (which the webpage fails to define, hmmm)
			
			// so here's the infinite product series from Wikipedia instead
			
			// Gamma(z) == (1/z) * Prod(n=1;n<inf;n++) {  (1+1/n)^z / (1+z/n) }
			
			const Int32 iterations = 25;
			
			BigNumFactory f = z.Factory;
			
			BigNum ret = z.Power(-1);
			
			for(int n=1;n<iterations;n++) {
				
				Double numerator1 = 1 + (1/n);
				BigNum numerator2 = Pow( f.Create( numerator1 ), z );
				
				BigNum denominato = f.Unity + z / f.Create(n);
				
				BigNum prodThis = numerator2 / denominato;
				
				ret *= prodThis;
			}
			
			return ret;
		}
Пример #51
0
 /// <summary>Converts a BigNum from one internal representation to another. If the type is the same then it returns a clone.</summary>
 public abstract BigNum Create(BigNum value);
Пример #52
0
		public static BigNum Tan(BigNum theta) {
			
			// using Tan(x) == Sin(x) / Cos(x)
			
			BigNum sine = Sin(theta);
			BigNum cosi = Cos(theta);
			
			return sine / cosi;
		}
Пример #53
0
 protected override BigNum Multiply(BigNum multiplicand)
 {
     return BigFloat.Multiply(this, (BigFloat)multiplicand);
 }
Пример #54
0
		public static BigNum Csc(BigNum theta) {
			return Sin(theta).Power(-1);
		}
Пример #55
0
		public static BigNum Sec(BigNum theta) {
			return Cos(theta).Power(-1);
		}
Пример #56
0
		public static BigNum Cot(BigNum theta) {
			return Tan(theta).Power(-1);
		}
Пример #57
0
 public DNumber(decimal value)
 {
     _value = BigFloatFactory.Instance.Create(value);
 }
Пример #58
0
		public static BigNum Max(BigNum x, BigNum y) {
			
			return x < y ? y : x;
		}
Пример #59
0
        public BigNum Evaluate(Dictionary<String,BigNum> symbols)
        {
            lock( _lock ) {

            ////////////////////////////
            // Reinit

            _valueStack.Clear();
            _operatorStack.Clear();
            _operatorStack.Push(Operator.Eof );

            _token  = Operator.Eof;
            _ptoken = Operator.Eof;
            _value  = _factory.Zero;
            _isFirstToken = true;
            _toki         = 0;

            if( symbols == null ) symbols = new Dictionary<String,BigNum>();

            _symbols = symbols;

            Advance();

            while(true) {

                OnStackChanged();

                if( _token == Operator.Val ) {
                    // if the current token is a value
                    // shift it to value stack

                    Shift();

                } else {

                    // get precedence for the last operator and the current operator
                    Operator lastOp = _operatorStack.Peek();
                    Precedence p = _precedence[ (int)lastOp, (int)_token ];
                    switch(p) {
                        case Precedence.Reduce:

                            Reduce();

                            break;
                        case Precedence.Shift:

                            Shift();

                            break;
                        case Precedence.Accept:

                            EnsureVal(1);

                            return _value = _valueStack.Pop();

                        default:
                            throw new ExpressionException( p.ToString() + " at position " + _toki );
                    }

                }

            }//while

            }//lock
        }
Пример #60
0
        private static BigNum EvaluateFunction(Function function, BigNum num)
        {
            switch(function) {

                case Function.Sin: return BigMath.Sin( num );
                case Function.Cos: return BigMath.Cos( num );
                case Function.Tan: return BigMath.Tan( num );

                case Function.Csc: return BigMath.Csc( num );
                case Function.Sec: return BigMath.Sec( num );
                case Function.Cot: return BigMath.Cot( num );

                case Function.Abs:   return BigMath.Abs( num );
                case Function.Ceil:  return BigMath.Ceiling( num );
                case Function.Floor: return BigMath.Floor( num );
                case Function.Fact:  return BigMath.Gamma( num );

                default:
                    throw new ExpressionException("Unknown or invalid function");

            }
        }