Search code examples
python-3.xgmpy

How to get abs() of an mpz in GMPY2?


I need to get the absolute value of an mpz object in GMPY2 but I cannot find any function like abs(). How can this be done?


Solution

  • mpz objects provide __abs__, and so the ordinary abs works:

    >>> gmpy2.mpz(3)
    mpz(3)
    >>> abs(gmpy2.mpz(3))
    mpz(3)
    >>> gmpy2.mpz(-3)
    mpz(-3)
    >>> abs(gmpy2.mpz(-3))
    mpz(3)