I just discovered, to my embarrassment, that suppressing negative metrics before mpz_pow_ui does not work very well. (βThe manual does say unsigned, you know.β) For other mpz_pow functions, the manual uses concepts that I donβt understand. For example, "base ^ exp mod mod" is as follows:
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.
In the following code, what do I need to change so that it can handle negative metrics?
#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; }
source share