4

令我尴尬的是,我刚刚发现,向负指数提供负指数mpz_pow_ui效果不佳。(“手册确实说无符号长,你知道。”)对于其他mpz_pow功能,手册使用了我不理解的概念。例如下面的“ base ^ exp mod mod ”:

void mpz_powm (mpz_t rop, mpz_t base, mpz_t exp, mpz_t mod) 
void mpz_powm_ui (mpz_t rop, mpz_t base, unsigned long int exp, mpz_t mod)
Set _rop_ to _base_^_exp_ mod _mod_.
Negative exp is supported if an inverse base-1 mod mod exists (see mpz_invert in Section 5.9 [Number Theoretic Functions], page 35). If an inverse doesn’t exist then a divide by zero is raised.

在以下代码中,我必须进行哪些更改才能使其能够处理负指数?

#define Z(x) mpz_t x; mpz_init( x );

BSTR __stdcall IBIGPOWER(BSTR p1, long p2 ) {
    USES_CONVERSION;

    Z(n1);
    Z(res);

    LPSTR sNum1 = W2A( p1 );

    mpz_set_str( n1, sNum1, 10 );

    mpz_pow_ui( res, n1, p2 );

    char * buff =  (char *) _alloca( mpz_sizeinbase( res, 10 ) + 2 );

    mpz_get_str(buff, 10, res);

    BSTR bResult = _com_util::ConvertStringToBSTR( buff );
    return bResult;
}
4

5 回答 5

9

我不会为你删减代码,但我会让你知道:

2-n = 1/2n

因此,您可以只传递正指数,然后将 1 除以该数字(并选择非整数类型,例如mpf_t-mpz_t类型是整数,因此不能表示实数,例如)。2-18

于 2009-01-07T03:22:12.683 回答
7

mpz_t数据类型只能存储整数,2 -18不是整数。要计算它,您必须使用浮点类型mpf_t或有理数类型mpq_t

于 2009-01-07T03:28:55.667 回答
2

我对 GMP 了解不多,但是:

2 ^ -18

相当于:

1 / (2 ^ 18)

那么为什么不写一个以这种方式处理负指数的函数呢?

于 2009-01-07T03:23:37.657 回答
1

如果存在逆 base-1 mod mod,则支持负 exp(参见第 5.9 节 [数论函数],第 35 页中的 mpz_invert)。如果逆不存在,则引发除以零。

If you're talking about that, that's involves number theory. Division, or more precisely inverse of multplication, only exists on certain conditions. I don't exactly remember the rules, but basically it's saying that the division operation won't work if base-1 mod mod doesn't exist.

于 2009-01-08T01:45:56.903 回答
0

您需要做什么取决于您希望在操作中丢失的位发生什么。由于您正在处理整数,因此提高到负幂意味着除法(嗯,互惠),但 GMP 提供了几种除法形式。

于 2009-01-07T03:24:01.340 回答