public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); RealExpr z = ctx.MkRealConst("z"); RatNum zero = ctx.MkReal(0); RatNum two = ctx.MkReal(2); Goal g = ctx.MkGoal(); g.Assert(ctx.MkGe(ctx.MkSub(ctx.MkPower(x, two), ctx.MkPower(y, two)), zero)); Probe p = ctx.MkProbe("num-consts"); Probe p2 = ctx.Gt(p, ctx.Const(2)); Tactic t = ctx.Cond(p2, ctx.MkTactic("simplify"), ctx.MkTactic("factor")); Console.WriteLine(t[g]); g = ctx.MkGoal(); g.Assert(ctx.MkGe(ctx.MkAdd(x, x, y, z), zero)); g.Assert(ctx.MkGe(ctx.MkSub(ctx.MkPower(x, two), ctx.MkPower(y, two)), zero)); Console.WriteLine(t[g]); } }
public static decimal AsDecimal([NotNull] this RatNum number) { var numerator = (decimal)number.BigIntNumerator; var denominator = (decimal)number.BigIntDenominator; return(numerator / denominator); }
public void testTwoArgCtor() { RatNum one_I_two = new RatNum(1, 2); eq(one_I_two, "1/2"); RatNum two_I_one = new RatNum(2, 1); eq(two_I_one, "2"); RatNum three_I_two = new RatNum(3, 2); eq(three_I_two, "3/2"); RatNum negOne_I_thirteen = new RatNum(-1, 13); eq(negOne_I_thirteen, "-1/13"); RatNum fiftyThree_I_seven = new RatNum(53, 7); eq(fiftyThree_I_seven, "53/7"); RatNum zero_I_one = new RatNum(0, 1); eq(zero_I_one, "0"); }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); RatNum zero = ctx.MkReal(0); RatNum two = ctx.MkReal(2); Goal g = ctx.MkGoal(); g.Assert(ctx.MkGt(x, zero)); g.Assert(ctx.MkGt(y, zero)); g.Assert(ctx.MkEq(x, ctx.MkAdd(y, two))); Console.WriteLine(g); Tactic t1 = ctx.MkTactic("simplify"); Tactic t2 = ctx.MkTactic("solve-eqs"); Tactic t = ctx.AndThen(t1, t2); Console.WriteLine(t[g]); } }
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); }
/** @requires: arg != null * @return a new RatNum equal to (this / arg). * If arg is zero, or if either argument is NaN, then returns NaN. */ public RatNum div(RatNum arg) { // (a/b) / (x/y) = ay/bx if (arg.isNaN()) { return(arg); } else { return(new RatNum(this.numer * arg.denom, this.denom * arg.numer)); } }
public static void Main(string[] args) { RationalNumber RatNum1 = new RationalNumber(); RationalNumber RatNum2 = new RationalNumber(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine())); RationalNumber RatNum; RatNum = RatNum1 + RatNum2; Console.WriteLine(RatNum.GetNumerator()); Console.WriteLine(RatNum.GetDenominator()); Console.WriteLine(RatNum1 > RatNum2); Console.WriteLine(RatNum1 != RatNum2); }
public void testApprox() { approxEq(zero.approx(), 0.0); approxEq(one.approx(), 1.0); approxEq(negOne.approx(), -1.0); approxEq(two.approx(), 2.0); approxEq(one_I_two.approx(), 0.5); approxEq(two_I_three.approx(), 2.0 / 3.0); approxEq(three_I_four.approx(), 0.75); // cannot test that one_I_zero.approx() approxEq double.NaN, // because it WON'T!!! Instead, construct corresponding // instance of double and use .Equals(..) method /* * assertTrue( (new System.Double(double.NaN)). * Equals * (new double(one_I_zero.approx())) ); * assertTrue( (new double(double.NaN)). * Equals * (new double(negOne_I_zero.approx())) ); * assertTrue( (new double(double.NaN)). * Equals * (new double(two_I_zero.approx())) ); * assertTrue( (new double(double.NaN)). * Equals * (new double(negTwo_I_zero.approx())) ); * assertTrue( (new double(double.NaN)). * Equals * (new double(hundred_I_zero.approx())) ); * assertTrue( (new double(double.NaN)). * Equals * (new double(negOne_I_zero.approx())) ); * * assertTrue( (new double(double.NaN)). * Equals * (new double(nine_I_zero.approx())) ); */ // use left-shift operator "<<" to create integer for 2^30 RatNum one_I_twoToThirty = new RatNum(1, (1 << 30)); double quiteSmall = 1.0 / System.Math.Pow(2, 30); approxEq(one_I_twoToThirty.approx(), quiteSmall); }
/** @requires: rn != null * @return a negative number if this < rn, * 0 if this = rn, * a positive number if this > rn. */ public int CompareTo(RatNum rn) { if (this.isNaN() && rn.isNaN()) { return(0); } else if (this.isNaN()) { return(1); } else if (rn.isNaN()) { return(-1); } else { RatNum diff = this.sub(rn); return(diff.numer); } }
public void testOneArgCtor() { eq(zero, "0"); eq(one, "1"); RatNum four = new RatNum(4); eq(four, "4"); eq(negOne, "-1"); RatNum negFive = new RatNum(-5); eq(negFive, "-5"); RatNum negZero = new RatNum(-0); eq(negZero, "0"); }
public void Run() { Dictionary <string, string> cfg = new Dictionary <string, string>() { { "AUTO_CONFIG", "true" } }; Context ctx = new Context(cfg); RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); RealExpr z = ctx.MkRealConst("z"); RatNum two = ctx.MkReal(2); Console.WriteLine(ctx.MkAdd(ctx.MkSub(ctx.MkMul(x, y), ctx.MkPower(y, two)), ctx.MkPower(z, two))); Console.WriteLine(ctx.MkSub(ctx.MkAdd(ctx.MkMul(x, y), ctx.MkPower(y, two)), ctx.MkPower(z, two))); Console.WriteLine(ctx.MkMul(ctx.MkAdd(x, y), z)); }
/** Standard equality operation. * @return true if and only if 'obj' is an instance of a RatNum * and 'this' = 'obj'. Note that NaN = NaN for RatNums. */ public override bool Equals(object obj) { if (obj is RatNum) { RatNum rn = (RatNum)obj; // special case: check if both are NaN if (this.isNaN() && rn.isNaN()) { return(true); } else { return(this.numer == rn.numer && this.denom == rn.denom); } } else { return(false); } }
public void testTwoArgCtorOnNaN() { RatNum one_I_zero = new RatNum(1, 0); eq(one_I_zero, "NaN"); RatNum two_I_zero = new RatNum(2, 0); eq(two_I_zero, "NaN"); RatNum negOne_I_zero = new RatNum(-1, 0); eq(negOne_I_zero, "NaN"); RatNum zero_I_zero = new RatNum(0, 0); eq(zero_I_zero, "NaN"); RatNum negHundred_I_zero = new RatNum(-100, 0); eq(negHundred_I_zero, "NaN"); }
public void testReduction() { RatNum negOne_I_negTwo = new RatNum(-1, -2); eq(negOne_I_negTwo, "1/2"); RatNum two_I_four = new RatNum(2, 4); eq(two_I_four, "1/2"); RatNum six_I_four = new RatNum(6, 4); eq(six_I_four, "3/2"); RatNum twentySeven_I_thirteen = new RatNum(27, 13); eq(twentySeven_I_thirteen, "27/13"); RatNum negHundred_I_negHundred = new RatNum(-100, -100); eq(negHundred_I_negHundred, "1"); }
/** @requires: c != null * @effects: * constructs a new RatTerm, t, with t.coeff = c and t.expt = e. */ public RatTerm(RatNum c, int e) { coeff = c; expt = e; }
// helper function, "decode-and-check" private void decChk(string s, RatNum expected) { eq(RatNum.parse(s), expected.unparse()); }
private void assertGreater(RatNum larger, RatNum smaller) { assertTrue(larger.CompareTo(smaller) > 0); assertTrue(smaller.CompareTo(larger) < 0); }
/** @requires: arg != null * @return a new RatNum equal to (this - arg). * If either argument is NaN, then returns NaN. */ public RatNum sub(RatNum arg) { // a/b - x/y = a/b + -x/y return(this.add(arg.negate())); }
/** @requires: arg != null * @return a new RatNum equal to (this * arg). * If either argument is NaN, then returns NaN. */ public RatNum mul(RatNum arg) { // (a/b) * (x/y) = ax/by return(new RatNum(this.numer * arg.numer, this.denom * arg.denom)); }
/** @requires: arg != null * @return a new RatNum equal to (this + arg). * If either argument is NaN, then returns NaN. */ public RatNum add(RatNum arg) { // a/b + x/y = ay/by + bx/by = (ay + bx)/by return(new RatNum(this.numer * arg.denom + arg.numer * this.denom, this.denom * arg.denom)); }
public static Rational ToRational(RatNum r) { return(ToRational(r.BigIntNumerator) / ToRational(r.BigIntDenominator)); }
private void assertNeg(RatNum n) { assertTrue(n.isNegative()); assertTrue(!n.isPositive()); }
public static Decimal CreateDecimal(RatNum val) { return(new Decimal(new BigDecimal(val.longValue()))); }
// self-explanatory helper function private void eq(RatNum ratNum, string rep) { assertEquals(rep, ratNum.unparse()); }