简体   繁体   中英

BigInteger modPow with BigDecimal exponent

I need a calculation of a BigInteger modPow, but with a BigDecimal as exponent.

Converting to double or using BigDecimal.pow and then mod is not possible in this case, since the full result without the mod will not fit into memory. (and double lacks precision) So converting to BigInteger with proper scale is out of question too.

I have found no library or similar for that.

Is it even mathematically possible to do an efficient modPow with a fractioned exponent without calculating the full exponentiation before?

EDIT: Example

BigInteger base = BigInteger.valueOf("101");
BigDecimal exp = BigDecimal.valueOf("24.387207613444534);
BigInteger mod = BigInteger.valueOf("10403");

BigInteger result = base.modPow(exp, mod)

These sample numbers are very small, the intended use needs much bigger ones. Its supposed to deliver the mod of a number that itself is too large to hold in RAM, but is log and an integer divisor are known.

So i found a way in the meantime. base ^ (floor(exp - 1)) as regular modPow multiplied with base ^ (1+ (exp - floor(exp)) Then floor and mod. The last exponentiation with factor 1.x is still small enough to fit into RAM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM