Skip to content

Commit

Permalink
Allow conversion with any precision.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkskeller committed Jul 2, 2024
1 parent c671787 commit a271f54
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions Compiler/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4579,6 +4579,14 @@ def __init__(self, _v=None, k=None, f=None, size=None):
self.f = f
assert k is not None
assert f is not None
def adjust(v):
f_diff = v.f - f
v = v.v
if f_diff < 0:
v <<= -f_diff
elif f_diff > 0:
v >>= f_diff
return v
if _v is None:
self.v = self.int_type(0)
elif isinstance(_v, self.int_type):
Expand All @@ -4593,21 +4601,14 @@ def __init__(self, _v=None, k=None, f=None, size=None):
elif isinstance(_v, type(self)):
self.v = _v.v
elif isinstance(_v, cfix):
assert _v.f <= self.f
self.v = self.int_type(_v.v << (self.f - _v.f))
self.v = self.int_type(adjust(_v))
elif isinstance(_v, (MemValue, MemFix)):
#this is a memvalue object
self.v = type(self)(_v.read()).v
elif isinstance(_v, (list, tuple)):
self.v = self.int_type(list(self.conv(x).v for x in _v))
elif isinstance(_v, personal):
f_diff = _v._v.f - f
v = _v._v.v
if f_diff < 0:
v <<= -f_diff
elif f_diff > 0:
v >>= f_diff
self.v = self.int_type(personal(_v.player, v))
self.v = self.int_type(personal(_v.player, adjust(_v._v)))
else:
raise CompilerError('cannot convert %s to sfix' % _v)
if not isinstance(self.v, self.int_type):
Expand Down

0 comments on commit a271f54

Please sign in to comment.