0

After using gmpy2.floor() on an mpz variable:

 x = mpz(5)
 x = gmpy2.floor(x/256)

x has the type mpfr and not mpz anymore, even though to my understanding, floor always returns integers.

How can I avoid that?

I'm afraid using x = mpz(gmpy2.floor(x/256)) will reduce the performance, wont it?

netik
  • 1,736
  • 4
  • 22
  • 45

1 Answers1

1

gmpy2 wraps the MPFR library and it returns an mpfr as the result type.

See http://www.mpfr.org/mpfr-current/mpfr.html#Integer-Related-Functions

FYI, Python 2.x returns a float from math.floor. The behavior was changed for Python 3.

If you are looking for the floor of integer division, you can use //.

>>> gmpy2.mpz(123456789)//256
mpz(482253)
casevh
  • 11,093
  • 1
  • 24
  • 35
  • Thank you casevh. One last question: What if I'm doing something like `math.floor(gmpy2.log2(mpz(2**1024)))` which returns `mpfr('1024.0')` There is probably no way to get an mpz back right? – netik Feb 27 '17 at 20:43
  • I would avoid floating point operations as much as possible. How about `(2**1024).bit_length() - 1`? – casevh Feb 27 '17 at 21:15