英文:
Valid parameters for GMP's mpz_mod_ui
问题
mpz_mod_ui
的文档中声明其接口为:
unsigned long int mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long int d);
这实际上计算了 r = n % d
,其中 r
是一个小于 d
的非负整数,也是函数的结果;或者如果 d == 0
,则会导致除零操作。
是否可以将参数 r
设置为 NULL
,意味着我们不需要一个用于存储结果的 mpz_t
?
在C语言中,unsigned long int
不是固定大小。它至少是32位,但通常是64位。GMP是否支持具有不同大小的 unsigned long int
的平台?在所有支持的平台上,最大的有效 d
始终是 ~0UL
(或等效地,在C编译器的<limits.h>
中定义的 ULONG_MAX
)吗?
英文:
The documentation of mpz_mod_ui
states it's interface as:
unsigned long int mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long int d);
That essentially computes r = n % d
, which is a non-negative integer less than d
, also the function result; or cause a division by zero if d == 0
.
Is it OK to set the r
parameter to NULL
, meaning we do not need an mpz_t
set to the result?
In C, unsigned long int
is not fixed size. It's at-least-32-bit, but is often 64-bit. Is GMP supported on platforms with various size of unsigned long int
? Is the largest valid d
always ~0UL
(or I think equivalently ULONG_MAX
defined in <limits.h>
of a C compiler) on all supported platforms?
答案1
得分: 0
当使用 mpz_mod_ui
时,将 r
参数设置为 NULL,表示不需要一个设置为结果的 mpz_t
,这样做是可以的吗?
这不是建议的做法,有更好的方法。如在 评论 中所述,有一个可以计算 n % d
(其中 d
适合于 unsigned long int
)的函数:
unsigned long int mpz_fdiv_ui (const mpz_t n, unsigned long int d);
GMP 在具有不同大小的 unsigned long int
的平台上受支持吗?
是的。如 另一条评论 中所述,unsigned long
可能是 64 位或 32 位,这取决于 GMP 编译参数,请参阅 ABI 和 ISA 文档。然而,64 位的 unsigned long int
越来越受支持,在每个支持的平台上至少有一种选项可用。
英文:
> When using mpz_mod_ui
, is it OK to set the r
parameter to NULL, meaning we do not need an mpz_t set to the result?
That's not advisable, and there's a better way. As noted in comment, there is a function computing n % d
for d
that fits an unsigned long int
:
unsigned long int mpz_fdiv_ui (const mpz_t n, unsigned long int d);
> Is GMP supported on platforms with various size of unsigned long int
?
Yes. As noted in another comment, unsigned long
can be 64-bit or 32-bit depending on GMP compilation parameters, see the ABI and ISA documentation. However 64-bit unsigned long int
is increasingly supported, and is available on each supported platforms in at least one option.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论