3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-28 03:15:50 +00:00

Add support for Python Fraction object

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2012-12-28 13:39:34 -08:00
parent d0d48c7ce2
commit a51c6d125d
2 changed files with 19 additions and 0 deletions

View file

@ -8,6 +8,7 @@
from z3 import *
from z3core import *
from z3printer import *
from fractions import Fraction
def _to_numeral(num, ctx=None):
if isinstance(num, Numeral):
@ -170,6 +171,14 @@ class Numeral:
assert(self.is_integer())
return long(Z3_get_numeral_string(self.ctx_ref(), self.as_ast()))
def as_fraction(self):
""" Return a numeral (that is a rational) as a Python Fraction.
>>> Numeral("1/5").as_fraction()
Fraction(1, 5)
"""
assert(self.is_rational())
return Fraction(self.numerator().as_long(), self.denominator().as_long())
def approx(self, precision=10):
"""Return a numeral that approximates the numeral `self`.
The result `r` is such that |r - self| <= 1/10^precision