Skip to content

Commit

Permalink
fix numpy int size for windows OS.
Browse files Browse the repository at this point in the history
This fix #73 #76 and #85 in windows OS using numpy 32 bit integer type
  • Loading branch information
francof2a committed Feb 10, 2024
1 parent 785d943 commit 85d731c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions fxpmath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def cumsum(x, axis=None, out=None, out_like=None, sizing='optimal', method='raw'
"""
def _cumsum_raw(x, n_frac, **kwargs):
precision_cast = (lambda m: np.array(m, dtype=object)) if n_frac >= _n_word_max else (lambda m: m)
return np.cumsum(x.val, **kwargs) * precision_cast(2**(n_frac - x.n_frac))
return np.cumsum(precision_cast(x.val), **kwargs) * precision_cast(2**(n_frac - x.n_frac))

if not isinstance(x, Fxp):
x = Fxp(x)
Expand All @@ -591,7 +591,7 @@ def _cumprod_raw(x, n_frac, **kwargs):
precision_cast = (lambda m: np.array(m, dtype=object)) if n_frac >= _n_word_max else (lambda m: m)
pow_vals = n_frac - np.cumsum(np.ones_like(np.array(x)), axis=axis).astype(int) * x.n_frac
conv_factors = utils.int_array([2**pow_val for pow_val in precision_cast(pow_vals)])
return np.cumprod(x.val, **kwargs) * conv_factors
return np.cumprod(precision_cast(x.val), **kwargs) * precision_cast(conv_factors)

if not isinstance(x, Fxp):
x = Fxp(x)
Expand Down
5 changes: 4 additions & 1 deletion fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,10 @@ def set_val(self, val, raw=False, vdtype=None, index=None):
val = val.astype(object)
else:
val = val.astype(original_vdtype)
val_dtype = np.int64 if self.signed else np.uint64
if _n_word_max_ <= 32:
val_dtype = np.int32 if self.signed else np.uint32
else:
val_dtype = np.int64 if self.signed else np.uint64

# rounding and overflowing
new_val = self._round(val * conv_factor , method=self.config.rounding)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ def test_issue_76_v0_4_8():
y = np.cumsum(w)
assert np.all(y() == np.array([1, 2, 3, 4]))

# Increase word size to 96 bits
w = Fxp([1, 1, 1, 1], dtype='fxp-s96/0')
y = np.cumsum(w)
assert np.all(y() == np.array([1, 2, 3, 4]))

def test_issue_77_v0_4_8():
# Precision error when numpy.reshape

Expand Down

0 comments on commit 85d731c

Please sign in to comment.