mpz_sizeinbase

Kevin Ryde user42@zip.com.au
Sat, 02 Nov 2002 07:17:34 +1000


Minh Nguyen <md6nguyen@yahoo.com> writes:
>
> Now, suppose the return value of mpz_sizeinbase(op, base) is numdigs and I want to know the *exact* number of digits. The most straightforward way is to compare op with base^(numdigs-1) and then reduce numdigs by 1 if op is less than              base^(numdigs-1). Is there a better way?

Not that I know of.

Per remarks under "Binary to Radix" in the manual, I think examining
some of the high bits of "op" could give an exact answer more often,
to the point of "almost always", but in general it's a bit tricky
because the difference between "op" being n or n-1 digits can be only
in the very last few bits.

> By the way, is it too expensive to *always* return the exact number of digits when base is not 2 ? 

It already does so. :-)  (And it always did I think, but it's has only
recently been documented.)