mirror of
https://github.com/Z3Prover/z3
synced 2025-04-15 21:38:44 +00:00
Fix for FP numeral construction in the Python API. Fixes #386.
This commit is contained in:
parent
077e801590
commit
b0781a14cd
|
@ -8315,20 +8315,36 @@ def FPSort(ebits, sbits, ctx=None):
|
||||||
ctx = z3._get_ctx(ctx)
|
ctx = z3._get_ctx(ctx)
|
||||||
return FPSortRef(Z3_mk_fpa_sort(ctx.ref(), ebits, sbits), ctx)
|
return FPSortRef(Z3_mk_fpa_sort(ctx.ref(), ebits, sbits), ctx)
|
||||||
|
|
||||||
def _to_float_str(val):
|
def _to_float_str(val, exp=0):
|
||||||
if isinstance(val, float):
|
if isinstance(val, float):
|
||||||
return str(val)
|
v = val.as_integer_ratio()
|
||||||
|
num = v[0]
|
||||||
|
den = v[1]
|
||||||
|
rvs = str(num) + '/' + str(den)
|
||||||
|
res = rvs + 'p' +_to_int_str(exp)
|
||||||
elif isinstance(val, bool):
|
elif isinstance(val, bool):
|
||||||
if val:
|
if val:
|
||||||
return "1.0"
|
res = "1.0"
|
||||||
else:
|
else:
|
||||||
return "0.0"
|
res = "0.0"
|
||||||
elif _is_int(val):
|
elif _is_int(val):
|
||||||
return str(val)
|
res = str(val)
|
||||||
elif isinstance(val, str):
|
elif isinstance(val, str):
|
||||||
return val
|
inx = val.find('*(2**')
|
||||||
if __debug__:
|
if inx == -1:
|
||||||
_z3_assert(False, "Python value cannot be used as a double")
|
res = val
|
||||||
|
elif val[-1] == ')':
|
||||||
|
res = val[0:inx]
|
||||||
|
exp = str(int(val[inx+5:-1]) + int(exp))
|
||||||
|
else:
|
||||||
|
_z3_assert(False, "String does not have floating-point numeral form.")
|
||||||
|
elif __debug__:
|
||||||
|
_z3_assert(False, "Python value cannot be used to create floating-point numerals.")
|
||||||
|
if exp == 0:
|
||||||
|
return res
|
||||||
|
else:
|
||||||
|
return res + 'p' + exp
|
||||||
|
|
||||||
|
|
||||||
def fpNaN(s):
|
def fpNaN(s):
|
||||||
_z3_assert(isinstance(s, FPSortRef), "sort mismatch")
|
_z3_assert(isinstance(s, FPSortRef), "sort mismatch")
|
||||||
|
@ -8385,8 +8401,6 @@ def FPVal(sig, exp=None, fps=None, ctx=None):
|
||||||
if exp == None:
|
if exp == None:
|
||||||
exp = 0
|
exp = 0
|
||||||
val = _to_float_str(sig)
|
val = _to_float_str(sig)
|
||||||
val = val + 'p'
|
|
||||||
val = val + _to_int_str(exp)
|
|
||||||
return FPNumRef(Z3_mk_numeral(ctx.ref(), val, fps.ast), ctx)
|
return FPNumRef(Z3_mk_numeral(ctx.ref(), val, fps.ast), ctx)
|
||||||
|
|
||||||
def FP(name, fpsort, ctx=None):
|
def FP(name, fpsort, ctx=None):
|
||||||
|
|
Loading…
Reference in a new issue