From b8720ee23b4fdae87100b9410f9a26dd72f5d504 Mon Sep 17 00:00:00 2001 From: francof2a Date: Wed, 6 Oct 2021 09:47:18 -0300 Subject: [PATCH] vesion 0.4.4 Fix wrapping and scaling issue #44. --- changelog.txt | 4 ++++ fxpmath/__init__.py | 2 +- fxpmath/objects.py | 11 +++++++++-- tests/test_issues.py | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/changelog.txt b/changelog.txt index 0a8adde..27e178d 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +version 0.4.4 +-------------------------------------------------- +* Fix wrapping and scaling issue #44. + version 0.4.3 -------------------------------------------------- * Fxp adds `config` as optional init parameter (kwarg). diff --git a/fxpmath/__init__.py b/fxpmath/__init__.py index 2faaa11..d5a31ab 100644 --- a/fxpmath/__init__.py +++ b/fxpmath/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.4.3' +__version__ = '0.4.4' import sys import os diff --git a/fxpmath/objects.py b/fxpmath/objects.py index 1a52d6f..7fa00f1 100644 --- a/fxpmath/objects.py +++ b/fxpmath/objects.py @@ -166,6 +166,7 @@ def __init__(self, val=None, signed=None, n_word=None, n_frac=None, n_int=None, # scaling if self.scale is None: self.scale = kwargs.pop('scale', 1) if self.bias is None: self.bias = kwargs.pop('bias', 0) + self.scaled = True if self.scale != 1 or self.bias != 0 else False # check if val is a raw value if raw is None: raw = kwargs.pop('raw', False) @@ -542,9 +543,15 @@ def _format_inupt_val(self, val, return_sizes=False, raw=False): # scaling conversion self.scaled = False if self.scale is not None and self.bias is not None and not raw: - if self.scale != 1 or self.bias != 0: + if self.bias != 0: self.scaled = True - val = (val - self.bias) / self.scale + val = val - self.bias + if self.scale != 1: + self.scaled = True + val = val / self.scale + # check if it is a numpy array + if not isinstance(val, (np.ndarray, np.generic)): + val = np.array(val) if return_sizes: return val, vdtype, raw, signed, n_word, n_frac diff --git a/tests/test_issues.py b/tests/test_issues.py index e259806..32a7c05 100644 --- a/tests/test_issues.py +++ b/tests/test_issues.py @@ -52,12 +52,10 @@ def test_issue_14_v0_3_7(): True, n_word=128, n_frac=125, rounding='around', raw=True) assert d.bin() == '00000000000000000000001011101010110101011101010101010101010101010101010101010101001010101010101010101001010101001010101010101010' - def test_issue_15_v0_3_7(): x = Fxp('0xb', True, 10, 4) assert x.hex() == '0x00B' - def test_issue_17_v0_3_7(): a = Fxp(15, signed=False) b = a ** Fxp(2) @@ -157,4 +155,36 @@ def test_issue_41_v0_4_2(): def test_issue_42_v0_4_2(): b = Fxp(2, True, 4, 0, overflow='wrap') assert (b + 8)() == -6.0 - assert (b - 8)() == -6.0 \ No newline at end of file + assert (b - 8)() == -6.0 + +def test_issue_44_v0_4_3(): + # 1a + b = Fxp(20.5, False, n_word=5, scaling=1, bias=8) + assert b() == 20.5 + + # 1b + b = Fxp(20.5, False, n_word=4, scaling=1, bias=8) + assert b() == 20 + b = Fxp(20.5, False, n_word=4, n_frac=1, scaling=1, bias=8) + assert b() == 15.5 + + # 2 + zero = Fxp(0.0, False, n_word=5, overflow='wrap', scaling=1, bias=8) + assert zero() == 32 + zero = Fxp(0.0, False, n_word=5, n_frac=1, overflow='wrap', scaling=1, bias=8) + assert zero() == 16.0 + assert zero.upper == 23.5 + assert zero.lower == 8.0 + + # 3 + b = Fxp(0, False, 64, 0, overflow='wrap', bias=8) + assert b() == 2**64 + b = Fxp(8, False, 64, 0, overflow='wrap', bias=8) + assert b() == 8 + b = Fxp(2**64 + 7, False, 64, 0, overflow='wrap', bias=8) + assert b() == 2**64 + 7 + b = Fxp(2**64 + 8, False, 64, 0, overflow='wrap', bias=8) + assert b() == 8 + + b = Fxp(2**64+6, False, 64, 0, overflow='wrap', scaling=2, bias=8) + assert b() == 2**64+6 \ No newline at end of file