Microsoft.Scripting.Math.BigInteger.Power C# (CSharp) Method

Power() public method

public Power ( int exp ) : BigInteger
exp int
return BigInteger
    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;
    }

Usage Example

Esempio n. 1
0
 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));
 }