Search code examples
python-3.xgmpy

GMPY2 - How to avoid gmpy2.floor() returning mpfr instead of mpz?


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?


Solution

  • 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)