question on fixing the mpz_sizeinbase() command to work correctly

website.reader website.reader at protonmail.com
Sat Mar 16 21:28:39 CET 2024


Hello all:

I was using two types of number libraries, one was the GP-Pari number theory calculator and the other was gmp v6.3.0 of course

I was sorting numbers based upon the count of digits that each had. For rational numbers, I counted the digits in both the numerator and denominator and summed them.

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

Here's the suggested fix for this command in a C++ code unit

==== start code.cpp unit here below this line ====

#include <gmpxx.h>
#include <iostream>
#include "bit_cnt.h"

using namespace std;

int main(int argc, char* argv[]) {

mpz_class N(argv[1]);
const unsigned long base = 10;
size_t nbits = mpz_sizeinbase(N.get_mpz_t(), base);
mpz_class u = 0;
mpz_ui_pow_ui(u.get_mpz_t(), base, nbits-1);
if ( u > abs(N) ) { nbits -= 1; }

cout << N << " " << nbits << endl;

return(0);
}
========= end of code unit =======

I made it a C++ program to illustrate the fix.

Can this simple fix be placed into gmp? I trust it is not asking too much.

Randall


More information about the gmp-discuss mailing list