wangtongada / gmpy

Automatically exported from code.google.com/p/gmpy
GNU Lesser General Public License v3.0
0 stars 0 forks source link

strange outputs v 2.0.3 in case of bit_scan0() & to_binary() #87

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. >>> import gmpy2 as gm
2. >>> [gm.mpz('-120').bit_scan0(j) for j in range(12)]
3. [0, 1, 2, 4, 4, 5, 6, -1, None, None, None, None]

What is the expected output? What do you see instead?
Imho, there should not appear a negative index in the list.

What version of the product are you using? On what operating system?
gmpy2 2.0.3 x64 with PY 2.7.6x64 on Win7x64prof

Please provide any additional information below.
the output of to_binary seems strange to me as well: 
>>> gm.to_binary(gm.mpz('-120'))
'\x01\x02x'

Original issue reported on code.google.com by DerStroe...@googlemail.com on 15 Apr 2014 at 5:00

GoogleCodeExporter commented 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
I have released 2.0.4b1 which includes the change to bit_scan0().

Original comment by casevh on 5 Aug 2014 at 5:37