Skip to content

pow() of Fxp unsigned values does not work #17

@arnfol

Description

@arnfol

Hi! First of all, thanks for this module, great work!

I've noticed, that Fxp does not support the power of unsigned values. The following example

a = Fxp(15, signed=False)
b = a ** 2

Generates traceback:

Traceback (most recent call last):
  File "dpd_model.py", line 16, in <module>
    b = a ** 2
  File "/home/arnfol/anaconda3/lib/python3.8/site-packages/fxpmath/objects.py", line 608, in __pow__
    y = Fxp(self.get_val() ** n, signed=self.signed or n.signed, n_word=n_word, n_frac=n_frac)
AttributeError: 'int' object has no attribute 'signed'

Which is, obviously, because integer n does not have a signed field :) I think that or n.signed is not needed here, because the sign of the number in pow does not affect the resulting sign.

I also suppose that the __pow__ method does not generate correct n_word bit size for signed values. Rigth now it multiplies sign bit by n, which is not needed. I assume it should be like:

def __pow__(self, n):
    n_intg = self.n_word - self.n_frac - 1 if self.signed else self.n_word - self.n_frac
    n_frac = self.n_frac * n
    n_word = n_intg * n + n_frac + 1 if self.signed else n_intg * n + n_frac

    y = Fxp(self.get_val() ** n, signed=self.signed, n_word=n_word, n_frac=n_frac)
    return y

But I haven't tested it yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions