mpz_sizeinbase() bug

Vincent Lefevre vincent at vinc17.net
Wed Aug 19 10:27:20 UTC 2020


On 2020-08-19 10:39:01 +0200, Paul Zimmermann wrote:
>        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.

IMHO, it's a bad thing to say that one returns something, and later
say that this can be inexact. It could be improved to something like:

  Return the size of OP measured in number of digits in the given
  BASE, or this size + 1.  BASE can vary from 2 to 62.  If BASE is
  a power of 2, the result is always the exact size.  The sign of
  OP is ignored, just the absolute value is used. If OP is zero the
  return value is always 1.

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

I would say that this is rather inefficient since the exact result
can generally be determined from the first few digits.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-bugs mailing list