Comments (4)
The same issue leads to confusing error message for simple slice assignment:
>>> d = gmpy2.xmpz(0b1000)
>>> d[1:3] = gmpy2.xmpz(0b111)
>>> bin(d)
'0b1110'
>>> d[1:3] = [True,True]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: must specify sequence of bits
Expected
>>> d = numpy.array([0,0,0,1], dtype=bool)
>>> d[1:3] = [1,1,1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>> d[1:3] = [True,True]
>>> to_bin(d)
'1110'
Or
>>> d = [0,0,0,1]
>>> d[1:3] = [1,1,1]
>>> to_bin(d)
'11110'
>>> d = [0,0,0,1]
>>> d[1:3] = [1,1]
>>> to_bin(d)
'1110'
Original comment by `[email protected]` on 2 Jun 2010 at 11:49
from gmpy.
My use of True is an ugly hack. When getting bits from a number, GMP treats 0
as an
infinitely long number with all bits set to 0. This makes assigning a slice of
bits
to 0 very easy. Unfortunately, 1 is considered an infinitely long number with
the
first bit set to 1 and all remaining bits set to 0. From GMP's perspective, -1
is an
infinitely strong number with all bits set to 1. I'll remove the special
handling of
True. Then the sample code will need to use -1 (not intuitive either) or I need
to
introduce a bit_mask(n) function to return a number with n bits set. This would
probably be more useful anyway.
I haven't thought about supporting assignment from a list. Since I'm assigning
to
bit positions of a number, I thought it was reasonable that the bit positions
came
from another number.
The alternative is to only support setting a slice to either all 0 or all 1.
I'm open to suggestions.
Thanks for the feedback.
Original comment by casevh
on 2 Jun 2010 at 1:44
- Changed state: Accepted
from gmpy.
I changed slice assignments to only accept 0 or 1. It's not as flexible but
also not
as confusing. I think this is reasonable since gmpy2 isn't intended for bit
operations.
Original comment by casevh
on 4 Jun 2010 at 4:43
- Added labels: Priority-Low
- Removed labels: Priority-Medium
from gmpy.
Changed to allow getting and setting arbitrary sequences of bits. True and
False are no longer treated as special values. To provide an as-long-as-needed
source of 0 bits, use the value 0. To provide an as-long-as-needed source of 1
bits, use ~0 (or -1). This matches the behavior of GMP and of 2's complement
integers.
If you want to use a list of boolean values as input, see the new method pack():
>>> gmpy2.pack([True, False, True],1)
mpz(5)
Original comment by casevh
on 12 Aug 2010 at 5:42
- Changed state: Fixed
from gmpy.
Related Issues (20)
- Segmentation fault using mpz on OSX HOT 6
- Building wheels with GMP et al HOT 5
- Adding _sympy_ method to mpfr type for robust interoperability with sympy HOT 3
- Expose mpz_probab_prime_p() int return values from is_prime() HOT 5
- Missing methods on mpz c.f. int HOT 11
- "latest" documentation looks outdated HOT 7
- Documentation has invalid function signatures
- Fix vectorcall support on Python 3.7 HOT 1
- Use pytest for testing? HOT 6
- Incorrect interpretation of numbers with high bases HOT 3
- Incompatible (with int) parsing of the sign in the mpz constructor HOT 3
- pycharm inspection "Unexpected argument" for gmpy2.mpz(...) HOT 9
- gmpy2.xmpz bit reverse padding HOT 1
- Efficient fixed-base modular exponentiation with powmod_exp_list?
- Compiler warnings on Windows 2022 & msys2 HOT 3
- Argument order yn() and jn() inconsistent with MPFR and mpmath HOT 3
- 2 tests fail HOT 4
- Pickling is slow wrt int's HOT 2
- Breaking changes in Python 3.12.0a7 release HOT 4
- Add function csqrt HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gmpy.