Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect values when accessing sign bits through slicing #341

Open
vogr opened this issue Jul 28, 2022 · 3 comments
Open

Incorrect values when accessing sign bits through slicing #341

vogr opened this issue Jul 28, 2022 · 3 comments

Comments

@vogr
Copy link

vogr commented Jul 28, 2022

Hi, I encountered a situation that I think is probably a bug.

Accessing the sign bits of negative mpz and xmpz numbers gives different results when the access is done with simple indexing and when the access is done through slicing:

In [1]: from gmpy2 import mpz

In [2]: a = mpz(-1)

In [3]: a[4]
Out[3]: 1

In [5]: a[4:5]
Out[5]: mpz(0)

Expected result: slices of sign bits should give 11..1 for negative integers instead of 00..0.

Thank you in advance for your help!

Valentin

@vogr
Copy link
Author

vogr commented Jul 28, 2022

As a workaround I'm using bit_mask and bit shift:

from gmpy2 import mpz, bit_mask

# one bit
(a >> 4) & 1

# multi bit
(a >> 4) & bit_mask(3)

@casevh
Copy link
Collaborator

casevh commented Jul 29, 2022

This is an interesting issue. I need to think about the best solution.

Let's assume a is always a negative 'mpz'.

What should a[2:] return? Without a given stop value, this will effectively lock up a system, and eventually crash the interpreter, while it tries to return an infinite number. I think maintaining the existing behavior is best when no stop value is given.

If we assume that a stop value is present, the change looks easy on Python 3.6.1 and later. (New C-API functions introduced.) I need to see if there is a reasonable fix for older versions of Python.

@vogr
Copy link
Author

vogr commented Jul 29, 2022

That's interesting, I hadn't thought about the unbounded slices! I wonder if unbounded slices are really necessary though? As a user it seems a bit counter-intuitive to me that a[0:] and a are not the same

In [21]: a = xmpz(-2)

In [22]: a[0:]
Out[22]: mpz(2)

Maybe unbounded slices in getter expressions should be forbidden for consistency? A user can use bit_length() to be explicit if this is really what they want.

But i do see why unbounded slices could be useful on the LHS, looks like I found another edge case though, I'll open a second issue :)

Edit: and here's the issue report #342

skirpichev added a commit to skirpichev/gmpy that referenced this issue Jan 30, 2023
skirpichev added a commit to skirpichev/gmpy that referenced this issue Jan 30, 2023
skirpichev added a commit to skirpichev/gmpy that referenced this issue Jan 30, 2023
skirpichev added a commit to skirpichev/gmpy that referenced this issue Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants