Skip to content

Commit

Permalink
vesion 0.4.4
Browse files Browse the repository at this point in the history
Fix wrapping and scaling issue #44.
  • Loading branch information
francof2a committed Oct 6, 2021
1 parent 7ed5cc0 commit b8720ee
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 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.4.4
--------------------------------------------------
* Fix wrapping and scaling issue #44.

version 0.4.3
--------------------------------------------------
* Fxp adds `config` as optional init parameter (kwarg).
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.3'
__version__ = '0.4.4'

import sys
import os
Expand Down
11 changes: 9 additions & 2 deletions fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
36 changes: 33 additions & 3 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
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

0 comments on commit b8720ee

Please sign in to comment.