Skip to content

Commit

Permalink
version 0.3.5
Browse files Browse the repository at this point in the history
* fix wrap error for ndarrays.
  • Loading branch information
francof2a committed Jun 10, 2020
1 parent 9ca5eb2 commit 3869a3f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
version 0.3.5
--------------------------------------------------
* fix wrap error for ndarrays.

version 0.3.4
--------------------------------------------------
* fix wrap error for unsigned Fxp.
Expand Down
2 changes: 1 addition & 1 deletion fxpmath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.3.4'
__version__ = '0.3.5'

import sys
__maxsize__ = sys.maxsize
Expand Down
11 changes: 1 addition & 10 deletions fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,7 @@ def _overflow_action(self, new_val, val_min, val_max):
# val = np.array(max(val_min, min(val_max, new_val)))
val = utils.clip(new_val, val_min, val_max)
elif self.overflow == 'wrap':
if new_val.ndim == 0:
if not ((new_val <= val_max) & (new_val >= val_min)):
if self.signed:
val = utils.twos_complement_repr(new_val, self.n_word)
else:
val = new_val % (1 << self.n_word)
else:
val = new_val
else:
val = np.array([v if ((v <= val_max) & (v >= val_min)) else utils.twos_complement_repr(v, self.n_word) for v in new_val])
val = utils.wrap(new_val, val_min, val_max, self.signed, self.n_word)
return val

def _round(self, val, method='floor'):
Expand Down
13 changes: 12 additions & 1 deletion fxpmath/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,15 @@ def binary_xor(x, y, n_word=None):
@array_support
def clip(x, val_min, val_max):
x_clipped = np.array(max(val_min, min(val_max, x)))
return x_clipped
return x_clipped

@array_support
def wrap(x, val_min, val_max, signed, n_word):
if not ((x <= val_max) & (x >= val_min)):
if signed:
x_wrapped = twos_complement_repr(x, n_word)
else:
x_wrapped = x % (1 << n_word)
else:
x_wrapped = x
return x_wrapped
35 changes: 34 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,37 @@ def test_scaling():
assert x.upper == 12047.5
assert x.lower == 10000.0
assert x.precision == 0.5
assert x.get_status(str) == ''
assert x.get_status(str) == ''

def test_wrap():
x = Fxp(3.75, False, 4, 2, overflow='wrap')
assert x() == 3.75
assert x.status['overflow'] == False
assert x.status['underflow'] == False

x(4.0)
assert x() == 0.0
assert x.status['overflow'] == True
assert x.status['underflow'] == False

x.reset()
x(-0.25)
assert x() == 3.75
assert x.status['overflow'] == False
assert x.status['underflow'] == True

x = Fxp(3.75, True, 5, 2, overflow='wrap')
assert x() == 3.75
assert x.status['overflow'] == False
assert x.status['underflow'] == False

x(4.0)
assert x() == -4.0
assert x.status['overflow'] == True
assert x.status['underflow'] == False

x.reset()
x(-4.25)
assert x() == 3.75
assert x.status['overflow'] == False
assert x.status['underflow'] == True
7 changes: 6 additions & 1 deletion tests/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ def test_bugs_0_3_2():
def test_bugs_0_3_3():
# wrap error
x = Fxp(12.5, False, 11, 8, overflow='wrap')
assert x() == 4.5
assert x() == 4.5

def test_bugs_0_3_4():
# wrap error for ndarrays
x = Fxp([[1, 1]], False, 17 + 3, 9, overflow='wrap')
assert x().all() == np.array([[1, 1]]).all()

0 comments on commit 3869a3f

Please sign in to comment.