سؤال

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:

print RealVal('1/10000000000000000000000000000')
print simplify((RealVal('1/10') ** RealVal('10')) ** RealVal('10'))

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 API Z3_mk_numeral to accomplish that. This code is available in the z3.py file included in the Z3 distribution. The API Z3_mk_numeral expects 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 make RealVal(a) convenient for python users, we use the method str(a) to convert a into a string before invoking Z3_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 for solver.add(x <= RealVal(1e-4)).

Now, we can ask, why does the example works for 1e-4 but it doesn't for 1e-6. The problem is the implementation of str in Python. str(1e-4) returns the string "0.0001" in decimal notation which is supported by Z3 Z3_mk_numeral, but str(1e-6) returns the string "1e-6" in scientific notation which is not supported by Z3_mk_numeral.

print str(1e-4)
print str(1e-6)

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_numeral for the next version of Z3.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top