public BigInteger Power(int exp) { if (exp == 0) return One; if (exp < 0) { throw new ArgumentOutOfRangeException(MathResources.NonNegativePower); } BigInteger factor = this; BigInteger result = One; while (exp != 0) { if ((exp & 1) != 0) result = result * factor; if (exp == 1) break; // avoid costly factor.square() factor = factor.Square(); exp >>= 1; } return result; }
public static object Power(BigInteger/*!*/ self, int exponent) { // BigInteger doesn't handle negative exponents. if (exponent < 0) { return Power(self, (double)exponent); } return Protocols.Normalize(self.Power(exponent)); }