Skip to content

Commit

Permalink
fix sizing at mul, truediv and floordiv when operands are complex.
Browse files Browse the repository at this point in the history
Fix issue # 91
  • Loading branch information
francof2a committed Feb 10, 2024
1 parent bf94a97 commit a8e4b01
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 10 additions & 6 deletions fxpmath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,11 @@ def _mul_raw(x, y, n_frac):
if not isinstance(y, Fxp):
y = Fxp(y)

is_complex = x.vdtype == complex or y.vdtype == complex

signed = x.signed or y.signed
n_frac = x.n_frac + y.n_frac
n_word = x.n_word + y.n_word
n_word = x.n_word + y.n_word + int(is_complex)
n_int = n_word - int(signed) - n_frac
optimal_size = (signed, n_word, n_int, n_frac)

Expand Down Expand Up @@ -405,12 +407,13 @@ def _floordiv_raw_complex(x, y, n_frac):
if not isinstance(y, Fxp):
y = Fxp(y)

if x.vdtype == complex or y.vdtype == complex:
is_complex = x.vdtype == complex or y.vdtype == complex
if is_complex:
_floordiv_repr = _floordiv_repr_complex
_floordiv_raw = _floordiv_raw_complex

signed = x.signed or y.signed
n_int = x.n_int + y.n_frac + signed
n_int = x.n_int + y.n_frac + int(signed) + int(is_complex)
n_frac = 0
n_word = int(signed) + n_int + n_frac
optimal_size = (signed, n_word, n_int, n_frac)
Expand Down Expand Up @@ -444,12 +447,13 @@ def _truediv_raw_complex(x, y, n_frac):
if not isinstance(y, Fxp):
y = Fxp(y)

if x.vdtype == complex or y.vdtype == complex:
is_complex = x.vdtype == complex or y.vdtype == complex
if is_complex:
_truediv_raw = _truediv_raw_complex

signed = x.signed or y.signed
n_int = x.n_int + y.n_frac + signed
n_frac = x.n_frac + y.n_int
n_int = x.n_int + y.n_frac + int(signed) + int(is_complex)
n_frac = x.n_frac + y.n_int + int(is_complex)
n_word = int(signed) + n_int + n_frac
optimal_size = (signed, n_word, n_int, n_frac)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,16 @@ def test_issue_90_v0_4_9():

assert np.all(x.val.shape == np.array((4,)))
assert np.all(zz.val.shape == np.array((2,2)))


def test_issue_91_v0_4_10():
# Inaccuracy for certain complex multiplications

x = Fxp(-1-1j)
xa = np.array(-1-1j)

y = x * x
ya = xa * xa

assert np.all(y() == ya)

0 comments on commit a8e4b01

Please sign in to comment.