mpz_sizeinbase() bug

Paul Zimmermann Paul.Zimmermann at
Wed Aug 19 08:39:01 UTC 2020

       Dear Johannes,

if you read carefully the GMP documentation, you will see this is not a bug:

 -- Function: size_t mpz_sizeinbase (const mpz_t OP, int BASE)
     Return the size of OP measured in number of digits in the given
     BASE.  BASE can vary from 2 to 62.  The sign of OP is ignored, just
     the absolute value is used.  The result will be either exact or 1
     too big.  If BASE is a power of 2, the result is always exact.  If
     OP is zero the return value is always 1.

In this case mpz_sizeinbase(63, 10) is exact, and
mpz_sizeinbase(64, 10) is 1 too big.

If you want the exact size, use mpz_get_str() and strlen(),
or mpz_ui_pow_ui() and mpz_cmp().

Paul Zimmermann

> Date: Wed, 19 Aug 2020 02:21:51 +0200
> From: "Lebender, Johannes" <johannes.lebender at>
> Dear GMP maintainers,
> i found a possible bug in the mpz_sizeinbase() function. e.g.
> mpz_sizeinbase(63, 10) returns 2
> mpz_sizeinbase(64, 10) returns 3
> Attached to this mail you can find a detailed report with all 
> informations needed as described in 
> Many thanks for maintaining this library!
> best regards,
> Johannes Lebender
> [2:application/x-gzip Show Save:GMP_mpz_sizeinbase_bugreport.tar (15kB)]
> [3:text/plain Hide]
> _______________________________________________
> gmp-bugs mailing list
> gmp-bugs at

More information about the gmp-bugs mailing list