Beispiel #1
0
 public override Numeric power(IntNum y)
 {
     if (isOne())
         return this;
     if (isMinusOne())
         return y.isOdd () ? this : IntNum.one ();
     if (y.words == null && y.ival >= 0)
         return power (this, y.ival);
     if (isZero())
         return y.isNegative () ? RatNum.infinity(-1) : (RatNum) this;
     return base.power (y);
 }
Beispiel #2
0
 /** Return this raised to an integer power.
  * Implemented by repeated squaring and multiplication.
  * If y < 0, returns div_inv of the result. */
 public virtual Numeric power(IntNum y)
 {
     if (y.isNegative ())
         return power(IntNum.neg(y)).div_inv();
     Numeric pow2 = this;
     Numeric r = null;
     for (;;)  // for (i = 0;  ; i++)
         {
             // pow2 == x**(2**i)
             // prod = x**(sum(j=0..i-1, (y>>j)&1))
             if (y.isOdd())
                 r = r == null ? pow2 : r.mul (pow2);  // r *= pow2
             y = IntNum.shift (y, -1);
             if (y.isZero())
                 break;
             // pow2 *= pow2;
             pow2 = pow2.mul (pow2);
         }
     return r == null ? mul_ident() : r;
 }