-
Notifications
You must be signed in to change notification settings - Fork 86
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
Comments
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) |
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. |
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 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 |
Hi, I encountered a situation that I think is probably a bug.
Accessing the sign bits of negative
mpz
andxmpz
numbers gives different results when the access is done with simple indexing and when the access is done through slicing:Expected result: slices of sign bits should give
11..1
for negative integers instead of00..0
.Thank you in advance for your help!
Valentin
The text was updated successfully, but these errors were encountered: