Closed GoogleCodeExporter closed 8 years ago
Thanks for the report. I'm working on a fix for the bit_scan0() bug. It will be
several days before I release the new version.
Regarding gmpy2.to_binary(): it encodes a gmpy2 type (mpz, xmpz, mpfr, or mpc)
as a sequence of bytes that can be saved to disk or sent to another machine and
then gmpy2.from_binary() will recover the original value.
>>> gmpy2.from_binary(gmpy2.to_binary(gmpy2.mpz(-120)))
mpz(-120)
If you want the binary representation of a number, use either bin(mpz) or
mpz.digits().
>>> bin(gmpy2.mpz(-120))
'-0b1111000'
>>> gmpy2.mpz(-120).digits(2)
'-1111000'
Please let me know if you have any more questions.
Case
Original comment by casevh
on 16 Apr 2014 at 4:01
Dear Case,
thank you very much for your answer. I do not feed on gmpy2, so take
your time... no problem at all. I'm in charge of writing a short
summary of gmpy2 for German students until July. This intro will
contain a worked example of all the functions I found by
help('gmpy2').
I do not remember exactly, but on the PC I tested gmpy2 on (and to
which I shall return tomorrow), the strings returned by to_binary
eventually contained strange characters like exclamation marks. I
think it happened when I tried gmpy2.to_binary(gmpy2.const_pi()), for
example. Please see the trailing 'x' in the example I gave in the
issue report. I think the string returned from to_binary should only
be composed of '\xnn' substrings, shouldn't it?? Additionally, it
seems to be in big endian byte order, isn't it?
All the best
Georg
Original comment by DerStroe...@googlemail.com
on 16 Apr 2014 at 7:04
Hi Georg,
The internal format of the result of to_binary() is documented in the file
gmpy_binary.c. The first byte identifies the type: 1 is mpz, 2 is xmpz, 3 is
mpq, 4 is mpfr, and 5 is mpc. The second byte encodes some information about
the value. For an mpz, it indicates if the the number is equal to 0, greater
than 0, or less than 0. The remaining bytes are the value.
How it is formatted for display is left to Python's normal rules: standard
ASCII characters are displayed and all other characters are escaped.
>>> for i in gmpy2.to_binary(gmpy2.mpz(-120)): print(hex(i))
...
0x1
0x2
0x78
>>>
Case
Original comment by casevh
on 18 Apr 2014 at 4:34
I have released 2.0.4b1 which includes the change to bit_scan0().
Original comment by casevh
on 5 Aug 2014 at 5:37
Original issue reported on code.google.com by
DerStroe...@googlemail.com
on 15 Apr 2014 at 5:00