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); }
public void Sub() { BigNum num1 = new BigNum(1000000000); BigNum num2 = new BigNum(200000000000); BigNum num3 = new BigNum(1000000000 - 200000000000); Assert.AreEqual(num1 - num2, num3); }
public static BigNum suma(BigNum A, BigNum B) { BigNum res = new BigNum(0); res.przypisz(A); res.plus(B); return(res); }
public static BigNum iloraz(BigNum A, BigNum B) { BigNum res = new BigNum(0); res.przypisz(A); res.podziel(B); return(res); }
/// <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); }
public static BigNum iloczyn(BigNum A, BigNum B) { BigNum res = new BigNum(0); res.przypisz(A); res.razy(B); return(res); }
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 ) ); }
public void SingleParamConstructorWithValidString() { foreach (string s in _validConstructorStrings) { BigNum num = new BigNum(s); Assert.NotNull(num); } }
public void RemoveTralingDecimalPoint() { string s = "110."; s = BigNum.RemoveTrailingZeros(s); Console.WriteLine("s: " + s); Assert.AreEqual("110", s); }
public static BigNum roznica(BigNum A, BigNum B) { BigNum res = new BigNum(0); res.przypisz(A); res.minus(B); return(res); }
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); } }
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()); }
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); }
public void PruneLeadingZeros() { string pruned = BigNum.PruneLeadingZeros("0.07"); Assert.AreEqual(".07", pruned); pruned = BigNum.PruneLeadingZeros("000000010.07"); Assert.AreEqual("10.07", pruned); }
public void GreaterThanWithSecondParamNegative() { BigNum num1 = new BigNum("1"); BigNum num2 = new BigNum("-1"); bool result = (num1 > num2); Assert.IsTrue(result); }
public void GreaterThanWithFirstParamNegative() { BigNum num1 = new BigNum("-1"); BigNum num2 = new BigNum("1"); bool result = (num1 > num2); Assert.IsFalse(result); }
public void GreaterThanWithEqualPositiveDecimals() { BigNum num1 = new BigNum("1.2"); BigNum num2 = new BigNum("1.4"); bool result = (num1 > num2); Assert.IsFalse(result); }
public void DivisionWith() { BigNum num1 = new BigNum("27"); BigNum num2 = new BigNum("7"); BigNum sum = num1 / num2; Assert.NotNull(sum); }
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); }
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); }
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); }
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()); }
public void DivideSon() { BigNum myBigNum1 = new BigNum("1"); BigNum myBigNum2 = new BigNum("3"); BigNum quotient = myBigNum1 / myBigNum2; int x = 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); }
public void PruneTrailingZeros() { string pruned = BigNum.PruneTrailingZeros("0.789000001"); Assert.AreEqual("0.789000001", pruned); pruned = BigNum.PruneTrailingZeros("0.78900000"); Assert.AreEqual("0.789", pruned); }
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()); }
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()); }
public void GetRightHandSideOfNegativeNumberWithDecimal() { BigNum num = new BigNum("-12.34"); BigInteger expected = BigInteger.Parse("-34"); BigInteger actual = BigNum.GetRightOfDecimal(num); Assert.AreEqual(expected, actual); }
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(); }
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); }
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]; } }
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"); }
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 ); }
/// <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; }
public static BigNum Cos(BigNum theta) { // using Cos(x) == Sin(x + 90deg) theta += theta.Factory.HalfPi; return Sin( theta ); }
public static BigNum Pow(BigNum num, Int32 exponent) { return num.Power( exponent ); }
public Variable(String name, BigNum value) { Name = name; Value = value; }
public static BigNum Min(BigNum x, BigNum y) { return x > y ? y : x; }
public static Boolean IsInteger(BigNum x) { return x.Floor() == x; }
private DNumber(BigNum num) { _value = num; }
/// <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; }
/// <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 ); }
public DNumber(long value) { _value = BigFloatFactory.Instance.Create(value); }
public override BigNum Create(BigNum value) { throw new NotImplementedException(); }
///////////////////////////////////////// public virtual Boolean TryParse(String value, out BigNum number) { try { number = Create( value ); } catch(FormatException) { number = null; return false; } return true; }
public static BigNum Ceiling(BigNum num) { return num.Ceiling(); }
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; }
public static BigNum Floor(BigNum num) { return num.Floor(); }
public static BigNum Abs(BigNum num) { return num.Absolute(); }
///////////////////////////////////////// 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; }
/// <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);
public static BigNum Tan(BigNum theta) { // using Tan(x) == Sin(x) / Cos(x) BigNum sine = Sin(theta); BigNum cosi = Cos(theta); return sine / cosi; }
protected override BigNum Multiply(BigNum multiplicand) { return BigFloat.Multiply(this, (BigFloat)multiplicand); }
public static BigNum Csc(BigNum theta) { return Sin(theta).Power(-1); }
public static BigNum Sec(BigNum theta) { return Cos(theta).Power(-1); }
public static BigNum Cot(BigNum theta) { return Tan(theta).Power(-1); }
public DNumber(decimal value) { _value = BigFloatFactory.Instance.Create(value); }
public static BigNum Max(BigNum x, BigNum y) { return x < y ? y : x; }
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 }
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"); } }