GMP整数的位数 [英] Number of digits of GMP integer

查看:1099
本文介绍了GMP整数的位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来确定一个GMP整数有多少位数?我知道你可以通过日志确定它,但我想知道是否有一些内置到库中,我错过。我在手册中找到的唯一的是:



_mp_size肢体的数量,或表示负整数时的负数。
零表示为_mp_size设置为零,在这种情况下_mp_d数据未使用。



但我的印象是,我正在寻找。





124839 = 6位数。

解决方案

您可以使用 size_t mpz_sizeinbase(mpz_t op,int base)来获取要输出的字符数

size_t mpz_sizeinbase(mpz_t op,int base)

p>

返回以给定基数中的位数测量的op大小。基数可以从2到62. op的符号被忽略,只是使用绝对值。结果将是精确的或1太大。如果base是2的幂,结果总是准确的。如果op为零,则返回值始终为1.



此函数可用于确定将op转换为字符串时所需的空间。正确的分配量通常比mpz_sizeinbase返回的值多两个,一个减号和一个空终止符。


这样的东西:

  size_t sz = mpz_sizeinbase(myNum,10); 

应该是一个好的开始。



如果您想要确切大小,可以使用该值创建一个足够大的缓冲区,输出值到该缓冲区,然后执行 strlen 以获得更准确的大小,例如:

  size_t sz = mpz_sizeinbase(myNum,10) + 1; // allow for sign 
char * buff = malloc(sz + 1); // allow for`\0`
if(buff!= NULL){
gmp_sprintf(buff,%Zd,myNum)
sz = strlen(buff);
免费(buff);
}

请注意,这不是最有效的方法,因为它每次分配一个缓冲区想要找到长度,如果分配失败,它默认为最安全的大小,可能大于必要的大小。



另一种可能的方法是使用更安全的 snprintf 选项,因为它返回已经写入的字节数,并防止缓冲区溢出:

  char oneChar; 
int sz = gmp_snprintf(& oneChar,1,%Zd,myNum);

我没有测试,但它是一个骗子我用于



请注意,这两种 精确大小解决方案在前面都有一个可选的符号。如果你想真正计算数字,而不是字符,你应该调整(例如,如果数字小于零,减去一个大小)。


Is there an easy way to determine the number of digits a GMP integer has? I know you can determine it through a log, but I was wondering if there was something built into the library that I'm missing. The only thing I've found in the manual is:

_mp_size The number of limbs, or the negative of that when representing a negative integer. Zero is represented by _mp_size set to zero, in which case the _mp_d data is unused.

But I'm under the impression that is quite different than what I'm looking for.

i.e

124839 = 6 digits.

解决方案

You can use size_t mpz_sizeinbase (mpz_t op, int base) to get the number of characters to output the number as a string in a specific base.

size_t mpz_sizeinbase (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.

This function can be used to determine the space required when converting op to a string. The right amount of allocation is normally two more than the value returned by mpz_sizeinbase, one extra for a minus sign and one for the null-terminator.

So something along the lines of:

size_t sz = mpz_sizeinbase (myNum, 10);

should be a good start.

If you want the exact size, you can use that value to create a big enough buffer, output the value to that buffer, then do a strlen to get the more accurate size, something like:

size_t sz = mpz_sizeinbase (myNum, 10) + 1; // allow for sign
char *buff = malloc (sz + 1);               // allow for `\0`
if (buff != NULL) {
    gmp_sprintf (buff, "%Zd", myNum);
    sz = strlen (buff);
    free (buff);
}

Note that it's not the most efficient way since it allocates a buffer every time you want to find the length, and it defaults to the safest size if the allocation fails, which could be one larger than necessary.

Another possible way is to use the safer snprintf option, since that returns the number of bytes that would have been written, and prevents buffer overflow:

char oneChar;
int sz = gmp_snprintf (&oneChar, 1, "%Zd", myNum);

I haven't tested that specifically but it's a trick I've used for "regular" C-style printing before.

Note that both those "exact size" solutions include an optional sign at the front. If you want to truly count the digits rather then the characters, you should adjust for that (subtracting one from the size if the number is less than zero, for example).

这篇关于GMP整数的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆