Skip to content

Commit f647cd4

Browse files
committed
fix init and best_size calc for Win32 platforms
1 parent 16228d4 commit f647cd4

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

fxpmath/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.3.0'
1+
__version__ = '0.3.1'
22

33
import sys
44
__maxsize__ = sys.maxsize

fxpmath/objects.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,14 @@ def set_best_sizes(self, val=None, n_word=None, n_frac=None, max_error=1.0e-6, n
212212
if np.iscomplexobj(val):
213213
val = np.array([val.real, val.imag])
214214

215+
# define numpy integer type
216+
if self.signed:
217+
int_dtype = np.int64
218+
else:
219+
int_dtype = np.uint64
220+
215221
# find fractional parts
216-
frac_vals = np.abs(np.subtract(val, val.astype(int))).ravel()
222+
frac_vals = np.abs(np.subtract(val, val.astype(int_dtype))).ravel()
217223

218224
# n_frac estimation
219225
if n_frac is None:
@@ -233,7 +239,7 @@ def set_best_sizes(self, val=None, n_word=None, n_frac=None, max_error=1.0e-6, n
233239
n_frac = int(max(n_frac_calcs))
234240

235241
# max raw value (integer) estimation
236-
n_int = max( np.ceil(np.log2(np.max(np.abs( val*(1 << n_frac) + 0.5 )))).astype(int) - n_frac, 0)
242+
n_int = max( np.ceil(np.log2(np.max(np.abs( val*(1 << n_frac) + 0.5 )))).astype(int_dtype) - n_frac, 0)
237243

238244
# size assignement
239245
if n_word is None:
@@ -279,11 +285,11 @@ def set_val(self, val, raw=False, vdtype=None):
279285
if self.signed:
280286
val_max = (1 << (self.n_word-1)) - 1
281287
val_min = -val_max - 1
282-
val_dtype = 'int'
288+
val_dtype = np.int64
283289
else:
284290
val_max = (1 << self.n_word) - 1
285291
val_min = 0
286-
val_dtype = 'uint'
292+
val_dtype = np.uint64
287293

288294
# conversion factor
289295
if raw:
@@ -330,12 +336,15 @@ def astype(self, dtype=None):
330336
if dtype is None:
331337
dtype = self.vdtype
332338

333-
if dtype == float:
334-
val = self.val / 2.0**self.n_frac
335-
elif dtype == int or dtype == 'uint':
336-
val = self.val.astype(dtype) // 2**self.n_frac
337-
elif dtype == complex:
338-
val = (self.val.real + 1j * self.val.imag) / 2.0**self.n_frac
339+
if self.val is not None:
340+
if dtype == float or np.issubdtype(dtype, np.floating):
341+
val = self.val / 2.0**self.n_frac
342+
elif dtype == int or dtype == 'uint' or dtype == 'int' or np.issubdtype(dtype, np.integer):
343+
val = self.val.astype(dtype) // 2**self.n_frac
344+
elif dtype == complex or np.issubdtype(dtype, np.complexfloating):
345+
val = (self.val.real + 1j * self.val.imag) / 2.0**self.n_frac
346+
else:
347+
val = self.val / 2.0**self.n_frac
339348
else:
340349
val = None
341350

@@ -541,14 +550,14 @@ def __pow__(self, n):
541550
n_word = self.n_word * n
542551
n_frac = self.n_frac * n
543552

544-
y = Fxp(self.get_val() ** n, signed=self.signed or x.signed, n_word=n_word, n_frac=n_frac)
553+
y = Fxp(self.get_val() ** n, signed=self.signed or n.signed, n_word=n_word, n_frac=n_frac)
545554
return y
546555

547556
def __rpow__(self, n):
548557
n_word = self.n_word * n
549558
n_frac = self.n_frac * n
550559

551-
y = Fxp(n ** self.get_val(), signed=self.signed or x.signed, n_word=n_word, n_frac=n_frac)
560+
y = Fxp(n ** self.get_val(), signed=self.signed or n.signed, n_word=n_word, n_frac=n_frac)
552561
return y
553562

554563
__ipow__ = __pow__
@@ -565,7 +574,7 @@ def __rshift__(self, n):
565574

566575
def __lshift__(self, n):
567576
if self.shifting == 'expand':
568-
n_word = max(self.n_word, np.ceil(np.log2(np.abs(self.val)+0.5)).astype(int) + self.signed + n)
577+
n_word = max(self.n_word, int(np.max(np.ceil(np.log2(np.abs(self.val)+0.5)))) + self.signed + n)
569578
else:
570579
n_word = self.n_word
571580

tests/test_bugs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ def test_bugs_0_2_2():
4141
assert x() == -1.0
4242
assert x.n_word == 4
4343
assert x.signed == True
44-
assert x.n_frac == 2
44+
assert x.n_frac == 2
45+
46+
def test_bugs_0_3_0():
47+
# fail in Win32 because numpy astype(int) behavior
48+
x = Fxp(4.001)
49+
assert x() == 4.001

0 commit comments

Comments
 (0)