示例#1
0
const Integer Integer::power(const Integer a) const {
	if (a.compareTo(Integer::ZERO) < 0) {
		return Integer::ZERO;
	}
	if (a.compareTo(Integer::ZERO) == 0) {
		return Integer::ONE;
	}
	Integer ret(this->toString());
	Integer pow(a.toString());
	while (pow.compareTo(Integer::ONE) > 0) {
		ret = ret.multiply(*this);
		pow = pow.subtract(Integer::ONE);
	}
	return ret;
}
示例#2
0
const DivisionResult Integer::doDivide(const Integer a, const Integer b) throw (DivideByZeroException) {
	if (b.compareTo(Integer::ZERO) == 0) {
		throw DivideByZeroException();
	}	
	if (a.compareTo(Integer::ZERO) == 0) {
		return DivisionResult(Integer::ZERO, Integer::ZERO);
	}
	if (b.compareTo(Integer::ONE) == 0) {
		return DivisionResult(a, Integer::ZERO);
	}
	DivisionResult ret(Integer::ZERO, a.abs());
	int lengthDifference = ret.remainder.bytes.length() - b.abs().bytes.length();

	for (int i = lengthDifference; i >= 0; --i) { 	
		std::stringstream ss;
		ss << i;
		
		Integer base = Integer::TEN.power(ss.str());
		Integer temp = b.abs().multiply(base);
	
			while (temp.compareTo(ret.remainder) <= 0) {    
				ret.remainder = ret.remainder.subtract(temp);
				ret.result = ret.result.add(base);
			}
	}
  if (a.isPositive() != b.isPositive()) {
    ret.result = ret.result.negate();
		if (b.isPositive()) {
			ret.remainder = b.subtract(ret.remainder);
		} else {
			ret.remainder = b.add(ret.remainder);
		}
  } else {
		if (!b.isPositive()) {
			ret.remainder = ret.remainder.negate();
		}
	}
	return ret;
}