Z3: Is it possible to sum up a BitVec and a Real?

依然范特西╮ 提交于 2019-12-13 18:07:03

问题


I'm using Z3py to try to make some experiments on round-off error problem, it turns out that i have to sum up the a BitVec and a Real. However, when I try to do so, i get a 'sort mismatch' error. This is my code:

x = BitVecVal(8, 6)
y = Real('y')

solve(y + x == 5)

Is there a way to sum a BitVec and a Real? or just to get the Int value of BitVec?


回答1:


the Z3 C based API does contain conversion functions from bit-vectors to numerals (integers) and integers can be coerced to reals. However, the python API does not expose the relevant function directly, but you can wrap it:

x = BitVecVal(2,8)
y = Real('y')


def to_int(x):
    return ArithRef(Z3_mk_bv2int(x.ctx_ref(), x.as_ast(), 0), x.ctx)

print solve(to_int(x) + y == 5)



回答2:


You could convert the bit vector value into a signed long:

x = BitVecVal(8, 6)
y = Real('y')

solve(y + x.as_signed_long() == 5)
# [y = -3]

By the way, I found as_signed_long by inspecting y as one usually does in Python, namely, by print dir(y).



来源:https://stackoverflow.com/questions/16476658/z3-is-it-possible-to-sum-up-a-bitvec-and-a-real

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!