/*++ Copyright (c) 2012 Microsoft Corporation Module Name: Numeral.cs Abstract: Z3 Managed API: Numerals Author: Christoph Wintersteiger (cwinter) 2012-03-20 Notes: --*/ using System; using System.Diagnostics.Contracts; #if !FRAMEWORK_LT_4 using System.Numerics; #endif namespace Microsoft.Z3 { /// /// Integer Numerals /// [ContractVerification(true)] public class IntNum : IntExpr { #region Internal internal IntNum(Context ctx, IntPtr obj) : base(ctx, obj) { Contract.Requires(ctx != null); } #endregion /// /// Retrieve the 64-bit unsigned integer value. /// public UInt64 UInt64 { get { UInt64 res = 0; if (Native.Z3_get_numeral_uint64(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not a 64 bit unsigned"); return res; } } /// /// Retrieve the int value. /// public int Int { get { int res = 0; if (Native.Z3_get_numeral_int(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not an int"); return res; } } /// /// Retrieve the 64-bit int value. /// public Int64 Int64 { get { Int64 res = 0; if (Native.Z3_get_numeral_int64(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not an int64"); return res; } } /// /// Retrieve the int value. /// public uint UInt { get { uint res = 0; if (Native.Z3_get_numeral_uint(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not a uint"); return res; } } #if !FRAMEWORK_LT_4 /// /// Retrieve the BigInteger value. /// public BigInteger BigInteger { get { return BigInteger.Parse(this.ToString()); } } #endif /// /// Returns a string representation of the numeral. /// public override string ToString() { return Native.Z3_get_numeral_string(Context.nCtx, NativeObject); } } /// /// Rational Numerals /// [ContractVerification(true)] public class RatNum : RealExpr { /// /// The numerator of a rational numeral. /// public IntNum Numerator { get { Contract.Ensures(Contract.Result() != null); return new IntNum(Context, Native.Z3_get_numerator(Context.nCtx, NativeObject)); } } /// /// The denominator of a rational numeral. /// public IntNum Denominator { get { Contract.Ensures(Contract.Result() != null); return new IntNum(Context, Native.Z3_get_denominator(Context.nCtx, NativeObject)); } } #if !FRAMEWORK_LT_4 /// /// Converts the numerator of the rational to a BigInteger /// public BigInteger BigIntNumerator { get { IntNum n = Numerator; return BigInteger.Parse(n.ToString()); } } /// /// Converts the denominator of the rational to a BigInteger /// public BigInteger BigIntDenominator { get { IntNum n = Denominator; return BigInteger.Parse(n.ToString()); } } #endif /// /// Returns a string representation in decimal notation. /// /// The result has at most decimal places. public string ToDecimalString(uint precision) { return Native.Z3_get_numeral_decimal_string(Context.nCtx, NativeObject, precision); } /// /// Returns a string representation of the numeral. /// public override string ToString() { return Native.Z3_get_numeral_string(Context.nCtx, NativeObject); } #region Internal internal RatNum(Context ctx, IntPtr obj) : base(ctx, obj) { Contract.Requires(ctx != null); } #endregion } /// /// Bit-vector numerals /// [ContractVerification(true)] public class BitVecNum : BitVecExpr { /// /// Retrieve the 64-bit unsigned integer value. /// public UInt64 UInt64 { get { UInt64 res = 0; if (Native.Z3_get_numeral_uint64(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not a 64 bit unsigned"); return res; } } /// /// Retrieve the int value. /// public int Int { get { int res = 0; if (Native.Z3_get_numeral_int(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not an int"); return res; } } /// /// Retrieve the 64-bit int value. /// public Int64 Int64 { get { Int64 res = 0; if (Native.Z3_get_numeral_int64(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not an int64"); return res; } } /// /// Retrieve the int value. /// public uint UInt { get { uint res = 0; if (Native.Z3_get_numeral_uint(Context.nCtx, NativeObject, ref res) == 0) throw new Z3Exception("Numeral is not a uint"); return res; } } #if !FRAMEWORK_LT_4 /// /// Retrieve the BigInteger value. /// public BigInteger BigInteger { get { return BigInteger.Parse(this.ToString()); } } #endif /// /// Returns a string representation of the numeral. /// public override string ToString() { return Native.Z3_get_numeral_string(Context.nCtx, NativeObject); } #region Internal internal BitVecNum(Context ctx, IntPtr obj) : base(ctx, obj) { Contract.Requires(ctx != null); } #endregion } /// /// Algebraic numbers /// [ContractVerification(true)] public class AlgebraicNum : ArithExpr { /// /// Return a upper bound for a given real algebraic number. /// The interval isolating the number is smaller than 1/10^. /// /// /// the precision of the result /// A numeral Expr of sort Real public RatNum ToUpper(uint precision) { Contract.Ensures(Contract.Result() != null); return new RatNum(Context, Native.Z3_get_algebraic_number_upper(Context.nCtx, NativeObject, precision)); } /// /// Return a lower bound for the given real algebraic number. /// The interval isolating the number is smaller than 1/10^. /// /// /// /// A numeral Expr of sort Real public RatNum ToLower(uint precision) { Contract.Ensures(Contract.Result() != null); return new RatNum(Context, Native.Z3_get_algebraic_number_lower(Context.nCtx, NativeObject, precision)); } /// /// Returns a string representation in decimal notation. /// /// The result has at most decimal places. public string ToDecimal(uint precision) { Contract.Ensures(Contract.Result() != null); return Native.Z3_get_numeral_decimal_string(Context.nCtx, NativeObject, precision); } #region Internal internal AlgebraicNum(Context ctx, IntPtr obj) : base(ctx, obj) { Contract.Requires(ctx != null); } #endregion } }