Skip to content

Commit

Permalink
Fix negative number parsing in dtype string (issue #80).
Browse files Browse the repository at this point in the history
  • Loading branch information
francof2a committed Jan 19, 2024
1 parent 542bdd6 commit dd04714
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version 0.4.9
* Selection of `prefix` for binary and hexadecimal representations.
* Fix `cumsum` function bug when dealing with sizes bigger than 32 bits (windows) / 64 bits (linux) (issue #76).
* Fix `numpy.reshape` function handling. This function was returning optimal size instead of same by default (issue #77).
* Fix negative number parsing in `dtype` string (issue #80).

version 0.4.8
--------------------------------------------------
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.4.9-dev7'
__version__ = '0.4.9-dev8'

import sys
import os
Expand Down
4 changes: 2 additions & 2 deletions fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ def get_dtype(self, notation=None):
return self._dtype

def _qfmt(self):
return re.compile(r'(s|u|q|uq|qu)(\d+)(\.\d+)?')
return re.compile(r'(s|u|q|uq|qu)(\d+)(\.[+-]?\d+)?')

def _fxpfmt(self):
return re.compile(r'fxp-(s|u)(\d+)/(\d+)(-complex)?')
return re.compile(r'fxp-(s|u)(\d+)/([+-]?\d+)(-complex)?')

def _parseformatstr(self, fmt):
fmt = fmt.casefold()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,19 @@ def test_issue_77_v0_4_8():
y = np.reshape(x, (1, 4))
# fxp-s4/3-complex
assert y.signed == True and y.n_word == 5 and y.n_frac == 3

def test_issue_80_v0_4_8():
# Creation of Fxp-object with negative n_frac

# The following code results in unexpected behaviour
# when trying to specify the same type using alternative formats
x = Fxp(16, signed=True, n_word=8, n_frac=-2)
# -> x.dtype = 'fxp-s8/-2' , ok
assert x.dtype == 'fxp-s8/-2'

x = Fxp(16, dtype='S10.-2')
assert x.dtype == 'fxp-s8/-2'

x = Fxp(16, dtype='fxp-s8/-2')
assert x.dtype == 'fxp-s8/-2'

0 comments on commit dd04714

Please sign in to comment.