Skip to content

Issue with wrapping #20

@MaenMallah

Description

@MaenMallah

Hello again,

As I spent some time debugging an issue and finally found the reason, I thought I can let you know with my potential solution for it.

Wrapping is not working when values are negative.

How to replicate:

x = Fxp(0, signed=True, n_word = 4, n_frac = 0)

x.equal(-30) ==> -14 which is not possible as we can only represent the range [-8,7]
This happens because the value I gave should wrap twice but the functions don't account for this. The positive case is correct.

To solve this I changed the wrap function inside utils.py

def wrap(x, val_min, val_max, signed, n_word):
    if not ((x <= val_max) & (x >= val_min)):
        if signed:
            x_wrapped = x_wrapped = twos_complement_repr(x, n_word)
            while x_wrapped < val_min:
                x_wrapped = twos_complement_repr(x_wrapped, n_word)
        else:
            x_wrapped = x % (1 << n_word)
    else:
        x_wrapped = x
    return x_wrapped

However, I am not sure if I broke something else.

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