Git Product home page Git Product logo

Comments (4)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 27, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.