question on fixing the mpz_sizeinbase() command to work correctly

marco.bodrato at tutanota.com marco.bodrato at tutanota.com
Sun Mar 17 19:46:55 CET 2024


Ciao Randall,

16 mar 2024, 23:50 da website.reader at protonmail.com:

> I was sorting numbers based upon the count of digits that each had.
>

How many numbers? Lots of them?


> However things didn't work right and I had to fix my code which used the gmp v6.3 mpz_sizeinbase() command.
>

Great, after reading the manual, and the definition of the function, you was able to correctly use its result. That's fine!


> Here's the suggested fix for this command in a C++ code unit
>
> ==== start code.cpp unit here below this line ====
>

[just a handful of lines of simple code]


> Can this simple fix be placed into gmp?
>

Your short code shows why there is no need to change the library.
The GMP function is very fast giving, as documented, an approximation of the actual number of digits needed to represent the given number with the given base.
Moreover, it is easy for the user to write a few lines of code to adjust the possible error.
Your code is a brilliant example.

Since you seem to need to classify many numbers, are you sure that the best way is to compute every time, again and again, the ui_pow_ui? Maybe it's more efficient to compute it once and compare it with a bunch of numbers giving the same result with mpz_sizeinbase, isn't it? Instead of recomputing the power, for the next number of digit, it is more efficient to update it... 5000 digits, 5001 digits, 5002 digits... with a fast small multiplication.
It would be impossible to implement such an optimization if it the ui_pow_ui was hardcoded in the library, for each call of the function. Should the library let optimizations became more difficult to achieve?

Should that function in the library be changed? The answer is no.

Ĝis,
mb


More information about the gmp-discuss mailing list