/// <summary> /// 计算指数 /// </summary> /// <param name="times"></param> /// <param name="precision">记录计算过程中的精度(即原数乘了10^precision倍)</param> public void Pow(int times, int precision = 0) { if (times < 0) { Reset(); } else if (times == 0) { SetABit(precision); } else { HugeNumber cache = new HugeNumber(this); HugeNumber temp = One; temp.MoveLeft(precision); SetABit(precision); int p = 0; for (int i = 0; i < 32; i++) { if ((times & (1 << i)) == 0) { continue; } while (p <= i) { temp.Multiply(cache); temp.MoveRight(precision); cache.SetValue(temp); p++; } Multiply(temp); MoveRight(precision); } } }
public void Divide(HugeNumber num) { if (num.IsZero) { throw new System.Exception(string.Format("{0} divided by 0 or null was not allowed.", this)); } if (IsZero) { return; } if (CompareAbsTo(num) < 0) { Reset(); return; } mNegative = mNegative ^ num.mNegative; HugeNumber tmp = new HugeNumber(this); tmp.Abs(); HugeNumber div = new HugeNumber(num); div.Abs(); Reset(); HugeNumber sum = Zero; while (tmp.CompareTo(div) >= 0) { int mov = tmp.mOffset - div.mOffset; div.MoveLeft(mov); if (tmp.CompareTo(div) < 0) { mov--; div.MoveRight(1); } tmp.Substract(div); if (tmp.CompareTo(Zero) >= 0) { sum.SetABit(mov); Add(sum); div.MoveRight(mov); } else { break; } } }