def test_is_methods(self): from fractions import Fraction as pyFraction self.assertFalse(is_pysmt_fraction(4)) self.assertTrue(is_pysmt_fraction(Fraction(4))) self.assertFalse(is_pysmt_integer(4.0)) self.assertTrue(is_pysmt_integer(Integer(4))) self.assertTrue(is_python_integer(int(2))) if PY2: self.assertTrue(is_python_integer(long(2))) if HAS_GMPY: from gmpy2 import mpz self.assertTrue(is_python_integer(mpz(1))) if HAS_GMPY: from gmpy2 import mpz, mpq self.assertTrue(is_python_rational(mpz(1))) self.assertTrue(is_python_rational(mpq(1))) if PY2: self.assertTrue(is_python_rational(long(1))) self.assertTrue(is_python_rational(pyFraction(5))) self.assertTrue(is_python_rational(3)) self.assertTrue(is_python_boolean(True)) self.assertTrue(is_python_boolean(False))
def test_is_methods(self): self.assertFalse(is_pysmt_fraction(4)) self.assertTrue(is_pysmt_fraction(Fraction(4))) self.assertFalse(is_pysmt_integer(4.0)) self.assertTrue(is_pysmt_integer(Integer(4))) self.assertTrue(is_python_integer(int(2))) if PY2: self.assertTrue(is_python_integer(long(2))) if HAS_GMPY: from gmpy2 import mpz self.assertTrue(is_python_integer(mpz(1))) if HAS_GMPY: from gmpy2 import mpz, mpq self.assertTrue(is_python_rational(mpz(1))) self.assertTrue(is_python_rational(mpq(1))) if PY2: self.assertTrue(is_python_rational(long(1))) self.assertTrue(is_python_rational(pyFraction(5))) self.assertTrue(is_python_rational(3)) self.assertTrue(is_python_boolean(True)) self.assertTrue(is_python_boolean(False))
def Real(self, value): """ Returns a Real-type constant of the given value. value can be: - A Fraction(n,d) - A tuple (n,d) - A long or int n - A float - (Optionally) a mpq or mpz object """ if value in self.real_constants: return self.real_constants[value] if is_pysmt_fraction(value): val = value elif type(value) == tuple: val = Fraction(value[0], value[1]) elif is_python_rational(value): val = pysmt_fraction_from_rational(value) else: raise PysmtTypeError("Invalid type in constant. The type was:" + \ str(type(value))) n = self.create_node(node_type=op.REAL_CONSTANT, args=tuple(), payload=val) self.real_constants[value] = n return n
def walk_real_constant(self, formula): assert is_pysmt_fraction(formula.constant_value()), \ "The type was " + str(type(formula.constant_value())) # TODO: Remove this once issue 113 in gmpy2 is solved v = formula.constant_value() n, d = v.numerator, v.denominator if formula.constant_value().denominator == 1: self.write("%s.0" % n) else: self.write("%s/%s" % (n, d))
def walk_real_constant(self, formula): assert is_pysmt_fraction(formula.constant_value()), \ "The type was " + str(type(formula.constant_value())) # TODO: Remove this once issue 113 in gmpy2 is solved v = formula.constant_value() n,d = v.numerator, v.denominator if formula.constant_value().denominator == 1: self.write("%s.0" % n) else: self.write("%s/%s" % (n, d))