I’m trying to integrate z3py into my application. There are assertions that involve small real numbers, such as
solver.add(x <= 1e-6)
Then I got the following error:
File "~/src/solver/z3.py", line 2001, in __le__
a, b = _coerce_exprs(self, other)
File "~/src/solver/z3.py", line 846, in _coerce_exprs
b = s.cast(b)
File "~/src/solver/z3.py", line 1742, in cast
return RealVal(val, self.ctx)
File "~/src/solver/z3.py", line 2526, in RealVal
return RatNumRef(Z3_mk_numeral(ctx.ref(), str(val), RealSort(ctx).ast), ctx)
File "~/src/solver/z3core.py", line 1774, in Z3_mk_numeral
raise Z3Exception(lib().Z3_get_error_msg_ex(a0, err))
src.solver.z3types.Z3Exception: 'parser error'
While the assertion
solver.add(x <= 1e-4)
seems to be fine.
Therefore, I’m guessing that there is some sort of precision limitation in z3. If so, is there an option to let the first assertion go through?
Thanks.
There is no precision limitation in Z3. It can represent arbitrary precision rational numbers and algebraic numbers. Here is an example:
You can try it online at: http://rise4fun.com/Z3Py/ahJQ
The function
RealVal(a)converts a Python value into a Z3 real value. It uses the Z3 C APIZ3_mk_numeralto accomplish that. This code is available in thez3.pyfile included in the Z3 distribution. The APIZ3_mk_numeralexpects a string which encodes a rational number in decimal format (e.g., ‘1.3’) or fractional format (e.g., ‘1/3’). Note that, scientific notation is not supported. To makeRealVal(a)convenient for python users, we use the methodstr(a)to convertainto a string before invokingZ3_mk_numeral.Thus, users can also write
RealVal(1),RealVal(1.2), etc.The second bit of information is that the statement
solver.add(x <= 1e-4)is essentially a short hand forsolver.add(x <= RealVal(1e-4)).Now, we can ask, why does the example works for
1e-4but it doesn’t for1e-6. The problem is the implementation ofstrin Python.str(1e-4)returns the string"0.0001"in decimal notation which is supported by Z3Z3_mk_numeral, butstr(1e-6)returns the string"1e-6"in scientific notation which is not supported byZ3_mk_numeral.Here is a link with the example above: http://rise4fun.com/Z3Py/C02q
To avoid this issue, we should avoid numbers in scientific notation when creating Z3 expressions.
I will see if I have time to add support for scientific notation in
Z3_mk_numeralfor the next version of Z3.