GMP 整数位数
有没有一种简单的方法可以确定 GMP 整数的位数?我知道您可以通过日志确定它,但我想知道库中是否内置了一些我丢失的东西。我在手册中找到的唯一内容是:
_mp_size 四肢的数量,或表示负整数时的负数。 零由设置为零的 _mp_size 表示,在这种情况下,_mp_d 数据未使用。
但我的印象与我正在寻找的完全不同。
即
124839 = 6 位数字。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 size_t mpz_sizeinbase (mpz_t op, int base) 来获取字符数,以将数字输出为特定基数的字符串。
所以类似的事情:
应该是一个好的开始。
如果您想要精确大小,您可以使用该值创建一个足够大的缓冲区,将值输出到该缓冲区,然后执行
strlen
要获得更准确的大小,例如:请注意,这不是最有效的方法,因为每次您想要查找长度时它都会分配一个缓冲区,并且如果分配失败,它默认为最安全的大小,这可能是一个更大的大小比必要的。
另一种可能的方法是使用更安全的
snprintf
选项,因为它返回将要写入的字节数,并防止缓冲区溢出:我还没有专门测试过但这是我之前用于“常规”C 风格打印的技巧。
请注意,这两种“精确尺寸”解决方案的前面都包含一个可选标志。如果您想真正计算数字而不是字符,您应该对此进行调整(例如,如果数字小于零,则从大小中减一)。
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.So something along the lines of:
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: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: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).