求一个整数的次方

//求base的exponent次方,不能使用库函数,同时不需要考虑大数问题
bool flag_InvalideInput = false;
double Power(double base, int exponent)
{
	flag_InvalideInput = false;
	if (equal(base, 0.0) && exponent < 0)//特殊条件,单独考虑,底数为0,指数为负数
	{
		flag_InvalideInput = true;
		return 0.0;
	}
	unsigned in absExponent = (unsigned int)(exponent);
	if (exponent < 0)
		absExponent = (unsigned int)(-exponent);
	double result = PowerWithExpoent(base, absExponent);
	if (exponent < 0)
		result = 1.0 / result;
	return result;
}

double PowerWithExpoent(double base, unsigned int exponent)
{
	double result = 1.0;
	for (int i = 1; i < exponent; i++)
		result = base * result;
	return result;
}

bool equal(double num1, double num2)
{
	if ((num1 - num2) > 0.00000001 && (num1 - num2) < -0.00000001)
		return true;
	else
		return false;
}

你可能感兴趣的:(求一个整数的次方)