Search code examples
c++gmp

sizeinbase for mpz_class in GNU GMP


In C we have mpz_t and we can access the size in a base by

size_t mpz_sizeinbase (const mpz_t op, int base)

In mpz_class, I've tried

counter.sizeinbase(2);

that didn't work.

error: ‘mpz_class {aka class __gmp_expr<__mpz_struct [1], __mpz_struct [1]>}’ has no member named ‘sizeinbase’
      size_t size = (counter.sizeinbase(2) + CHAR_BIT-1) / CHAR_BIT;

I need to use mpz_class to simplify the code for unordered_map

is there a similar function for mpz_class or a workaround?


Solution

  • Use the get_mpz_t method to access the wrapped mpz_t value.

    mpz_class x = ...;
    size_t xbits = mpz_sizeinbase(x.get_mpz_t(), 2);
    

    At least this works in MPIR, I assume it works in GMP as well.