/** Calcaulte the simplest rational between two reals. */ public static RealNum rationalize(RealNum x, RealNum y) { // This algorithm is by Alan Bawden. It has been transcribed // with permission from C-Gambit, copyright Marc Feeley. if (x.grt(y)) { return(simplest_rational2(y, x)); } else if (!(y.grt(x))) { return(x); } else if (x.sign() > 0) { return(simplest_rational2(x, y)); } else if (y.isNegative()) { return((RealNum)(simplest_rational2((RealNum)y.neg(), (RealNum)x.neg())).neg()); } else { return(IntNum.zero()); } }
/** Return the logical (bit-wise) "and" of two IntNums. */ public static IntNum and(IntNum x, IntNum y) { if (y.words == null) { return(and(x, y.ival)); } else if (x.words == null) { return(and(y, x.ival)); } if (x.ival < y.ival) { IntNum temp = x; x = y; y = temp; } int i; int len = y.isNegative() ? x.ival : y.ival; int[] words = new int[len]; for (i = 0; i < y.ival; i++) { words[i] = x.words[i] & y.words[i]; } for ( ; i < len; i++) { words[i] = x.words[i]; } return(IntNum.make(words, len)); }
/** Converts an integral double (such as a toInt result) to an IntNum. */ public static IntNum toExactInt(double value) { if (Double.IsInfinity(value) || Double.IsNaN(value)) { throw new ArithmeticException("cannot convert " + value + " to exact integer"); } long bits = BitConverter.DoubleToInt64Bits(value); bool neg = bits < 0; int exp = (int)(bits >> 52) & 0x7FF; bits &= 0xfffffffffffffL; if (exp == 0) { bits <<= 1; } else { bits |= 0x10000000000000L; } if (exp <= 1075) { int rshift = 1075 - exp; if (rshift > 53) { return(IntNum.zero()); } bits >>= rshift; return(IntNum.make(neg ? -bits : bits)); } return(IntNum.shift(IntNum.make(neg ? -bits : bits), exp - 1075)); }
/** Return this raised to an integer power. * Implemented by repeated squaring and multiplication. * If y < 0, returns div_inv of the result. */ public virtual Numeric power(IntNum y) { if (y.isNegative()) { return(power(IntNum.neg(y)).div_inv()); } Numeric pow2 = this; Numeric r = null; for (;;) // for (i = 0; ; i++) { // pow2 == x**(2**i) // prod = x**(sum(j=0..i-1, (y>>j)&1)) if (y.isOdd()) { r = r == null ? pow2 : r.mul(pow2); // r *= pow2 } y = IntNum.shift(y, -1); if (y.isZero()) { break; } // pow2 *= pow2; pow2 = pow2.mul(pow2); } return(r == null?mul_ident() : r); }
/** Return true iff an IntNum and an int have any true bits in common. */ public static bool test(IntNum x, int y) { if (x.words == null) { return((x.ival & y) != 0); } return((y < 0) || (x.words[0] & y) != 0); }
public static double GetRealValue(ArithExpr expr, Context localContext) { RatNum xRatNum = expr.Simplify() as RatNum; IntNum d = xRatNum.Denominator; IntNum n = xRatNum.Numerator; BigRational bigRational = new BigRational(n.BigInteger, d.BigInteger); return((double)bigRational); }
/** Return the logical (bit-wise) "and" of an IntNum and an int. */ public static IntNum and(IntNum x, int y) { if (x.words == null) return IntNum.make (x.ival & y); if (y >= 0) return IntNum.make (x.words[0] & y); int len = x.ival; int[] words = new int[len]; words[0] = x.words[0] & y; while (--len > 0) words[len] = x.words[len]; return IntNum.make (words, x.ival); }
public static RatNum add(RatNum x, RatNum y, int k) { IntNum x_num = x.numerator(); IntNum x_den = x.denominator(); IntNum y_num = y.numerator(); IntNum y_den = y.denominator(); if (IntNum.equals(x_den, y_den)) { return(RatNum.make(IntNum.add(x_num, y_num, k), x_den)); } return(RatNum.make(IntNum.add(IntNum.times(y_den, x_num), IntNum.times(y_num, x_den), k), IntNum.times(x_den, y_den))); }
/** Return the value of a specified bit in an IntNum. */ public static bool bitValue(IntNum x, int bitno) { int i = x.ival; if (x.words == null) { return(bitno >= 32 ? i < 0 : ((i >> bitno) & 1) != 0); } else { int wordno = bitno >> 5; return(wordno >= i ? x.words[i - 1] < 0 : (((x.words[wordno]) >> bitno) & 1) != 0); } }
public static RatNum make(IntNum num, IntNum den) { IntNum g = IntNum.gcd(num, den); if (den.isNegative()) { g = IntNum.neg(g); } if (!g.isOne()) { num = IntNum.quotient(num, g); den = IntNum.quotient(den, g); } return(den.isOne() ? (RatNum)num : (RatNum)(new IntFraction(num, den))); }
/** Count one bits in an IntNum. * If argument is negative, count zero bits instead. */ public static int bitCount(IntNum x) { int i, x_len; int[] x_words = x.words; if (x_words == null) { x_len = 1; i = bitCount (x.ival); } else { x_len = x.ival; i = bitCount (x_words, x_len); } return x.isNegative () ? x_len * 32 - i : i; }
/** Count one bits in an IntNum. * If argument is negative, count zero bits instead. */ public static int bitCount(IntNum x) { int i, x_len; int[] x_words = x.words; if (x_words == null) { x_len = 1; i = bitCount(x.ival); } else { x_len = x.ival; i = bitCount(x_words, x_len); } return(x.isNegative() ? x_len * 32 - i : i); }
/** Convert rational to (rounded) integer, after multiplying by 10**k. */ public static IntNum toScaledInt(RatNum r, int k) { if (k != 0) { IntNum power = IntNum.power(IntNum.ten(), k < 0 ? -k : k); IntNum num = r.numerator(); IntNum den = r.denominator(); if (k >= 0) { num = IntNum.times(num, power); } else { den = IntNum.times(den, power); } r = RatNum.make(num, den); } return(r.toExactInt(ROUND)); }
/** Do one the the 16 possible bit-wise operations of two IntNums. */ public static IntNum bitOp(int op, IntNum x, IntNum y) { switch (op) { case 0: return(IntNum.zero()); case 1: return(and(x, y)); case 3: return(x); case 5: return(y); case 15: return(IntNum.minusOne()); } IntNum result = new IntNum(); setBitOp(result, op, x, y); return(result.canonicalize()); }
/** Return the logical (bit-wise) "and" of two IntNums. */ public static IntNum and(IntNum x, IntNum y) { if (y.words == null) return and (x, y.ival); else if (x.words == null) return and (y, x.ival); if (x.ival < y.ival) { IntNum temp = x; x = y; y = temp; } int i; int len = y.isNegative () ? x.ival : y.ival; int[] words = new int[len]; for (i = 0; i < y.ival; i++) words[i] = x.words[i] & y.words[i]; for ( ; i < len; i++) words[i] = x.words[i]; return IntNum.make (words, len); }
/** Return the logical (bit-wise) "and" of an IntNum and an int. */ public static IntNum and(IntNum x, int y) { if (x.words == null) { return(IntNum.make(x.ival & y)); } if (y >= 0) { return(IntNum.make(x.words[0] & y)); } int len = x.ival; int[] words = new int[len]; words[0] = x.words[0] & y; while (--len > 0) { words[len] = x.words[len]; } return(IntNum.make(words, x.ival)); }
private static RealNum simplest_rational2(RealNum x, RealNum y) { RealNum fx = x.toInt(FLOOR); RealNum fy = y.toInt(FLOOR); if (!x.grt(fx)) { return(fx); } else if (fx.Equals(fy)) { RealNum n = (RealNum)IntNum.one().div(y.sub(fy)); RealNum d = (RealNum)IntNum.one().div(x.sub(fx)); return((RealNum)fx.add(IntNum.one().div(simplest_rational2(n, d)), 1)); } else { return((RealNum)fx.add(IntNum.one(), 1)); } }
public override double doubleValue() { bool neg = num.isNegative(); if (den.isZero()) { return(neg ? Double.NegativeInfinity : num.isZero() ? Double.NaN : Double.PositiveInfinity); } IntNum n = num; if (neg) { n = IntNum.neg(n); } int num_len = n.intLength(); int den_len = den.intLength(); int exp = 0; if (num_len < den_len + 54) { exp = den_len + 54 - num_len; n = IntNum.shift(n, exp); exp = -exp; } // Divide n (which is shifted num) by den, using truncating division, // and return quot and remainder. IntNum quot = new IntNum(); IntNum remainder = new IntNum(); IntNum.divide(n, den, quot, remainder, TRUNCATE); quot = quot.canonicalize(); remainder = remainder.canonicalize(); return(quot.roundToDouble(exp, neg, !remainder.isZero())); }
/** Return true iff two IntNums have any true bits in common. */ public static bool test(IntNum x, IntNum y) { if (y.words == null) { return(test(x, y.ival)); } else if (x.words == null) { return(test(y, x.ival)); } if (x.ival < y.ival) { IntNum temp = x; x = y; y = temp; } for (int i = 0; i < y.ival; i++) { if ((x.words[i] & y.words[i]) != 0) { return(true); } } return(y.isNegative()); }
public override Numeric power(IntNum y) { // bool inv; // if (y.isNegative()) // { // inv = true; // y = IntNum.neg(y); // } // else // inv = false; // if (y.words == null) // { // IntNum num = IntNum.power (numerator(), y.ival); // IntNum den = IntNum.power (denominator(), y.ival); // return inv ? RatNum.make (den, num) : RatNum.make (num, den); // } // double d = doubleValue(); // bool neg = d < 0.0 && y.isOdd(); // d = Math.Pow (d, y.doubleValue()); // if (inv) // d = 1.0/d; return(null); }
/** Return true iff two IntNums have any true bits in common. */ public static bool test(IntNum x, IntNum y) { if (y.words == null) return test (x, y.ival); else if (x.words == null) return test (y, x.ival); if (x.ival < y.ival) { IntNum temp = x; x = y; y = temp; } for (int i = 0; i < y.ival; i++) { if ((x.words[i] & y.words[i]) != 0) return true; } return y.isNegative (); }
/** Return the logical (bit-wise) "exclusive or" of two IntNums. */ public static IntNum xor(IntNum x, IntNum y) { return bitOp (6, x, y); }
/** Do one the the 16 possible bit-wise operations of two IntNums. */ public static void setBitOp(IntNum result, int op, IntNum x, IntNum y) { if (y.words == null) { } else if (x.words == null || x.ival < y.ival) { IntNum temp = x; x = y; y = temp; op = swappedOp(op); } int xi; int yi; int xlen, ylen; if (y.words == null) { yi = y.ival; ylen = 1; } else { yi = y.words[0]; ylen = y.ival; } if (x.words == null) { xi = x.ival; xlen = 1; } else { xi = x.words[0]; xlen = x.ival; } if (xlen > 1) { result.realloc(xlen); } int[] w = result.words; int i = 0; // Code for how to handle the remainder of x. // 0: Truncate to length of y. // 1: Copy rest of x. // 2: Invert rest of x. int finish = 0; int ni; switch (op) { case 0: // clr ni = 0; break; case 1: // and for (;;) { ni = (int)((uint)xi & (uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) { finish = 1; } break; case 2: // andc2 for (;;) { ni = (int)((uint)xi & ~(uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) { finish = 1; } break; case 3: // copy x ni = xi; finish = 1; // Copy rest break; case 4: // andc1 for (;;) { ni = (int)(~(uint)xi & (uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) { finish = 2; } break; case 5: // copy y for (;;) { ni = yi; if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } break; case 6: // xor for (;;) { ni = (int)((uint)xi ^ (uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } finish = yi < 0 ? 2 : 1; break; case 7: // ior for (;;) { ni = (int)((uint)xi | (uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) { finish = 1; } break; case 8: // nor for (;;) { ni = (int)(~((uint)xi | (uint)yi)); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) { finish = 2; } break; case 9: // eqv [exclusive nor] for (;;) { ni = (int)(~((uint)xi ^ (uint)yi)); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } finish = yi >= 0 ? 2 : 1; break; case 10: // c2 for (;;) { ni = (int)(~(uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } break; case 11: // orc2 for (;;) { ni = (int)((uint)xi | ~(uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) { finish = 1; } break; case 12: // c1 ni = ~xi; finish = 2; break; case 13: // orc1 for (;;) { ni = (int)(~(uint)xi | (uint)yi); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) { finish = 2; } break; case 14: // nand for (;;) { ni = (int)(~((uint)xi & (uint)yi)); if (i + 1 >= ylen) { break; } w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) { finish = 2; } break; default: case 15: // set ni = -1; break; } // Here i==ylen-1; w[0]..w[i-1] have the correct result; // and ni contains the correct result for w[i+1]. if (i + 1 == xlen) { finish = 0; } switch (finish) { case 0: if (i == 0 && w == null) { result.ival = ni; return; } w[i++] = ni; break; case 1: w[i] = ni; while (++i < xlen) { w[i] = x.words[i]; } break; case 2: w[i] = ni; while (++i < xlen) { w[i] = ~x.words[i]; } break; } result.ival = i; }
/** Return the logical (bit-wise) "exclusive or" of two IntNums. */ public static IntNum xor(IntNum x, IntNum y) { return(bitOp(6, x, y)); }
/** Return the logical (bit-wise) negation of an IntNum. */ public static IntNum not(IntNum x) { return(bitOp(12, x, IntNum.zero())); }
/** Extract a bit-field as an unsigned integer. */ public static IntNum extract(IntNum x, int startBit, int endBit) { //System.err.print("extract(["); if (x.words!=null) MPN.dprint(x.words); //System.err.println (","+x.ival+"], start:"+startBit+", end:"+endBit); if (endBit < 32) { int word0 = x.words == null ? x.ival : x.words[0]; return IntNum.make ((word0 & ~((-1) << endBit)) >> startBit); } int x_len; if (x.words == null) { if (x.ival >= 0) return IntNum.make (startBit >= 31 ? 0 : (x.ival >> startBit)); x_len = 1; } else x_len = x.ival; bool neg = x.isNegative (); if (endBit > 32 * x_len) { endBit = 32 * x_len; if (!neg && startBit == 0) return x; } else x_len = (endBit + 31) >> 5; int length = endBit - startBit; if (length < 64) { long l; if (x.words == null) l = x.ival >> (startBit >= 32 ? 31 : startBit); else l = MPN.rshift_long (x.words, x_len, startBit); return IntNum.make (l & ~((-1L) << length)); } int startWord = startBit >> 5; // Allocate a work buffer, which has to be large enough for the result // AND large enough for all words we use from x (including possible // partial words at both ends). int buf_len = (endBit >> 5) + 1 - startWord; int[] buf = new int[buf_len]; if (x.words == null) // x < 0. buf[0] = startBit >= 32 ? -1 : (x.ival >> startBit); else { x_len -= startWord; startBit &= 31; MPN.rshift0 (buf, x.words, startWord, x_len, startBit); } x_len = length >> 5; buf[x_len] &= ~((-1) << length); return IntNum.make (buf, x_len + 1); }
/** Do one the the 16 possible bit-wise operations of two IntNums. */ public static IntNum bitOp(int op, IntNum x, IntNum y) { switch (op) { case 0: return IntNum.zero(); case 1: return and (x, y); case 3: return x; case 5: return y; case 15: return IntNum.minusOne(); } IntNum result = new IntNum (); setBitOp (result, op, x, y); return result.canonicalize (); }
/** Extract a bit-field as an unsigned integer. */ public static IntNum extract(IntNum x, int startBit, int endBit) { //System.err.print("extract(["); if (x.words!=null) MPN.dprint(x.words); //System.err.println (","+x.ival+"], start:"+startBit+", end:"+endBit); if (endBit < 32) { int word0 = x.words == null ? x.ival : x.words[0]; return(IntNum.make((word0 & ~((-1) << endBit)) >> startBit)); } int x_len; if (x.words == null) { if (x.ival >= 0) { return(IntNum.make(startBit >= 31 ? 0 : (x.ival >> startBit))); } x_len = 1; } else { x_len = x.ival; } bool neg = x.isNegative(); if (endBit > 32 * x_len) { endBit = 32 * x_len; if (!neg && startBit == 0) { return(x); } } else { x_len = (endBit + 31) >> 5; } int length = endBit - startBit; if (length < 64) { long l; if (x.words == null) { l = x.ival >> (startBit >= 32 ? 31 : startBit); } else { l = MPN.rshift_long(x.words, x_len, startBit); } return(IntNum.make(l & ~((-1L) << length))); } int startWord = startBit >> 5; // Allocate a work buffer, which has to be large enough for the result // AND large enough for all words we use from x (including possible // partial words at both ends). int buf_len = (endBit >> 5) + 1 - startWord; int[] buf = new int[buf_len]; if (x.words == null) // x < 0. { buf[0] = startBit >= 32 ? -1 : (x.ival >> startBit); } else { x_len -= startWord; startBit &= 31; MPN.rshift0(buf, x.words, startWord, x_len, startBit); } x_len = length >> 5; buf[x_len] &= ~((-1) << length); return(IntNum.make(buf, x_len + 1)); }
/** Return the logical (bit-wise) negation of an IntNum. */ public static IntNum not(IntNum x) { return bitOp (12, x, IntNum.zero ()); }
/* Assumes x and y are both canonicalized. */ public static bool equals(RatNum x, RatNum y) { return(IntNum.equals(x.numerator(), y.numerator()) && IntNum.equals(x.denominator(), y.denominator())); }
public static IntFraction neg(IntFraction x) { // If x is normalized, we do not need to call RatNum.make to normalize. return(new IntFraction(IntNum.neg(x.numerator()), x.denominator())); }
public static int compare(RatNum x, RatNum y) { return(IntNum.compare(IntNum.times(x.numerator(), y.denominator()), IntNum.times(y.numerator(), x.denominator()))); }
/** Return exact "rational" infinity. * @param sign either 1 or -1 for positive or negative infinity */ public static RatNum infinity(int sign) { return(new IntFraction(IntNum.make(sign), IntNum.zero())); }
/** Return the logical (bit-wise) "(inclusive) or" of two IntNums. */ public static IntNum ior(IntNum x, IntNum y) { return bitOp (7, x, y); }
/** Return true iff an IntNum and an int have any true bits in common. */ public static bool test(IntNum x, int y) { if (x.words == null) return (x.ival & y) != 0; return (y < 0) || (x.words[0] & y) != 0; }
protected void Clique(Context ctx, Edge[] edges, uint n) { uint num = 0; foreach (Edge e in edges) { if (e.v0 >= num) { num = e.v0 + 1; } if (e.v1 >= num) { num = e.v1 + 1; } } Solver S = ctx.MkSolver(); IntExpr [] In = new IntExpr[num]; for (uint i = 0; i < num; i++) { In[i] = ctx.MkIntConst(String.Format("in_{0}", i)); S.Assert(ctx.MkLe(ctx.MkInt(0), In[i])); S.Assert(ctx.MkLe(In[i], ctx.MkInt(1))); } ArithExpr sum = ctx.MkInt(0); foreach (IntExpr e in In) { sum = ctx.MkAdd(sum, e); } S.Assert(ctx.MkGe(sum, ctx.MkInt(n))); IntNum[][] matrix = new IntNum[num][]; for (uint i = 0; i < num; i++) { matrix[i] = new IntNum[i]; for (uint j = 0; j < i; j++) { matrix[i][j] = ctx.MkInt(0); } } foreach (Edge e in edges) { uint s = e.v0; uint t = e.v1; if (s < t) { s = e.v1; t = e.v0; } matrix[s][t] = ctx.MkInt(1); } for (uint i = 0; i < num; i++) { for (uint j = 0; j < i; j++) { if (i != j) { if (matrix[i][j].Int == 0) { S.Assert(ctx.MkOr(ctx.MkEq(In[i], ctx.MkInt(0)), ctx.MkEq(In[j], ctx.MkInt(0)))); } } } } Status r = S.Check(); if (r == Status.UNSATISFIABLE) { Console.WriteLine("no solution"); } else if (r == Status.UNKNOWN) { Console.Write("failed"); Console.WriteLine(S.ReasonUnknown); } else { Console.WriteLine("solution found"); Model m = S.Model; Console.Write("{ "); foreach (FuncDecl cfd in m.ConstDecls) { IntNum q = (IntNum)m.ConstInterp(cfd); if (q.Int == 1) { Console.Write(" " + cfd.Name); } } Console.WriteLine(" }"); Console.Write("{ "); for (uint i = 0; i < num; i++) { IntNum q = (IntNum)m.Evaluate(In[i]); if (q.Int == 1) { Console.Write(" " + In[i]); } } Console.WriteLine(" }"); } }
public static void Test() { // Тесты для IntNum System.Console.WriteLine("Tests for IntNum" + "\n"); string input = "0"; System.Console.WriteLine(input + ":"); Lexer L = new IntNum(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = ""; System.Console.WriteLine("Empty string:"); L = new IntNum(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "253"; System.Console.WriteLine(input + ":"); L = new IntNum(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "-271273"; System.Console.WriteLine(input + ":"); L = new IntNum(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "124-1373"; System.Console.WriteLine(input + ":"); L = new IntNum(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для Identifier System.Console.WriteLine("\n" + "Tests for Identifier"); input = ""; System.Console.WriteLine("Empty string:"); L = new Identifier(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "a"; System.Console.WriteLine(input + ":"); L = new Identifier(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "bnasbh1221bhiu"; System.Console.WriteLine(input + ":"); L = new Identifier(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "5"; System.Console.WriteLine(input + ":"); L = new Identifier(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "5asbdh28"; System.Console.WriteLine(input + ":"); L = new Identifier(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = ";adf22f"; System.Console.WriteLine(input + ":"); L = new Identifier(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для Int0 System.Console.WriteLine("\n" + "Tests for Int0"); input = ""; System.Console.WriteLine("Empty string:"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "-724"; System.Console.WriteLine(input + ":"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "213651"; System.Console.WriteLine(input + ":"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "abc"; System.Console.WriteLine(input + ":"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "-012"; System.Console.WriteLine(input + ":"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "0"; System.Console.WriteLine(input + ":"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "0931"; System.Console.WriteLine(input + ":"); L = new Int0(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для Alternation System.Console.WriteLine("\n" + "Tests for Alternation"); input = ""; System.Console.WriteLine("Empty string:"); L = new Alternation(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "a"; System.Console.WriteLine(input + ":"); L = new Alternation(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "1"; System.Console.WriteLine(input + ":"); L = new Alternation(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "b3n4n6"; System.Console.WriteLine(input + ":"); L = new Alternation(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "m2hr4"; System.Console.WriteLine(input + ":"); L = new Alternation(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "i8s74g"; System.Console.WriteLine(input + ":"); L = new Alternation(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для ListOfLetters System.Console.WriteLine("\n" + "Tests for ListOfLetters"); input = ""; System.Console.WriteLine("Empty string:"); L = new ListOfLetters(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "v;d:n:"; System.Console.WriteLine(input + ":"); L = new ListOfLetters(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = ":n;e;"; System.Console.WriteLine(input + ":"); L = new ListOfLetters(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "m"; System.Console.WriteLine(input + ":"); L = new ListOfLetters(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "fe:n:"; System.Console.WriteLine(input + ":"); L = new ListOfLetters(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "m;:w;w"; System.Console.WriteLine(input + ":"); L = new ListOfLetters(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для ListOfNumbers System.Console.WriteLine("\n" + "Tests for ListOfNumbers"); input = ""; System.Console.WriteLine("Empty string:"); L = new ListOfNumbers(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "12 636 22 1 523"; System.Console.WriteLine(input + ":"); L = new ListOfNumbers(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = " 2 4 9"; System.Console.WriteLine(input + ":"); L = new ListOfNumbers(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "1; 3"; System.Console.WriteLine(input + ":"); L = new ListOfNumbers(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для Groups System.Console.WriteLine("\n" + "Tests for Groups"); input = ""; System.Console.WriteLine("Empty string:"); L = new Groups(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "aa12c23dd1"; System.Console.WriteLine(input + ":"); L = new Groups(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "aa122c23dd1"; System.Console.WriteLine(input + ":"); L = new Groups(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "aa 12v6"; System.Console.WriteLine(input + ":"); L = new Groups(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для DoubleLexer System.Console.WriteLine("\n" + "Tests for DoubleLexer"); input = ""; System.Console.WriteLine("Empty string:"); L = new DoubleLexer(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "3"; System.Console.WriteLine(input + ":"); L = new DoubleLexer(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "5241.32"; System.Console.WriteLine(input + ":"); L = new DoubleLexer(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "7265.27.37"; System.Console.WriteLine(input + ":"); L = new DoubleLexer(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = ".34"; System.Console.WriteLine(input + ":"); L = new DoubleLexer(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "782."; System.Console.WriteLine(input + ":"); L = new DoubleLexer(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для StringInQuotes System.Console.WriteLine("\n" + "Tests for StringInQuotes"); input = ""; System.Console.WriteLine("Empty string:"); L = new StringInQuotes(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "'dbj2134;sdh.adsn'"; System.Console.WriteLine(input + ":"); L = new StringInQuotes(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "'h6gsf2'sdj1d'"; System.Console.WriteLine(input + ":"); L = new StringInQuotes(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "''"; System.Console.WriteLine(input + ":"); L = new StringInQuotes(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "'vqhw2"; System.Console.WriteLine(input + ":"); L = new StringInQuotes(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "annx'"; System.Console.WriteLine(input + ":"); L = new StringInQuotes(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } // Тесты для Comment System.Console.WriteLine("\n" + "Tests for Comment"); input = ""; System.Console.WriteLine("Empty string:"); L = new Comment(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "/**/"; System.Console.WriteLine(input + ":"); L = new Comment(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "/*jbs.d3d431iu*/"; System.Console.WriteLine(input + ":"); L = new Comment(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "/*4odpus"; System.Console.WriteLine(input + ":"); L = new Comment(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "an;sjsof*/"; System.Console.WriteLine(input + ":"); L = new Comment(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } input = "*/msdc;w"; System.Console.WriteLine(input + ":"); L = new Comment(input); try { L.Parse(); } catch (LexerException e) { System.Console.WriteLine(e.Message); } }
public override IntNum toExactInt(int rounding_mode) { return(IntNum.quotient(numerator(), denominator(), rounding_mode)); }
/** Do one the the 16 possible bit-wise operations of two IntNums. */ public static void setBitOp(IntNum result, int op, IntNum x, IntNum y) { if (y.words == null) { } else if (x.words == null || x.ival < y.ival) { IntNum temp = x; x = y; y = temp; op = swappedOp (op); } int xi; int yi; int xlen, ylen; if (y.words == null) { yi = y.ival; ylen = 1; } else { yi = y.words[0]; ylen = y.ival; } if (x.words == null) { xi = x.ival; xlen = 1; } else { xi = x.words[0]; xlen = x.ival; } if (xlen > 1) result.realloc (xlen); int[] w = result.words; int i = 0; // Code for how to handle the remainder of x. // 0: Truncate to length of y. // 1: Copy rest of x. // 2: Invert rest of x. int finish = 0; int ni; switch (op) { case 0: // clr ni = 0; break; case 1: // and for (;;) { ni = (int)((uint)xi & (uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) finish = 1; break; case 2: // andc2 for (;;) { ni = (int)((uint)xi & ~(uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) finish = 1; break; case 3: // copy x ni = xi; finish = 1; // Copy rest break; case 4: // andc1 for (;;) { ni = (int)(~(uint)xi & (uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) finish = 2; break; case 5: // copy y for (;;) { ni = yi; if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } break; case 6: // xor for (;;) { ni = (int)((uint)xi ^ (uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } finish = yi < 0 ? 2 : 1; break; case 7: // ior for (;;) { ni = (int)((uint)xi | (uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) finish = 1; break; case 8: // nor for (;;) { ni = (int)(~((uint)xi | (uint)yi)); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) finish = 2; break; case 9: // eqv [exclusive nor] for (;;) { ni = (int)(~((uint)xi ^ (uint)yi)); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } finish = yi >= 0 ? 2 : 1; break; case 10: // c2 for (;;) { ni = (int)(~(uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } break; case 11: // orc2 for (;;) { ni = (int)((uint)xi | ~(uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) finish = 1; break; case 12: // c1 ni = ~xi; finish = 2; break; case 13: // orc1 for (;;) { ni = (int)(~(uint)xi | (uint)yi); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi >= 0) finish = 2; break; case 14: // nand for (;;) { ni = (int)(~((uint)xi & (uint)yi)); if (i+1 >= ylen) break; w[i++] = ni; xi = x.words[i]; yi = y.words[i]; } if (yi < 0) finish = 2; break; default: case 15: // set ni = -1; break; } // Here i==ylen-1; w[0]..w[i-1] have the correct result; // and ni contains the correct result for w[i+1]. if (i+1 == xlen) finish = 0; switch (finish) { case 0: if (i == 0 && w == null) { result.ival = ni; return; } w[i++] = ni; break; case 1: w[i] = ni; while (++i < xlen) w[i] = x.words[i]; break; case 2: w[i] = ni; while (++i < xlen) w[i] = ~x.words[i]; break; } result.ival = i; }
public static RatNum divide(RatNum x, RatNum y) { return(RatNum.make(IntNum.times(x.numerator(), y.denominator()), IntNum.times(x.denominator(), y.numerator()))); }
/** Return the value of a specified bit in an IntNum. */ public static bool bitValue(IntNum x, int bitno) { int i = x.ival; if (x.words == null) { return bitno >= 32 ? i < 0 : ((i >> bitno) & 1) != 0; } else { int wordno = bitno >> 5; return wordno >= i ? x.words[i-1] < 0 : (((x.words[wordno]) >> bitno) & 1) != 0; } }
public IntFraction(IntNum num, IntNum den) { this.num = num; this.den = den; }