면접 문제 16:수치의 정수 제곱

/*******************************************************************
*《  Offer——            》C++  
*
* htfeng
* 2018.09.27
*
*    16:        
*   :    double Power(double base, int exponent), base exponent
*   。       ,           。
*******************************************************************/

class Solution {
public:
	double PowerWithUnsignedExponent(double base, int absExponent);
	double Power(double base, int exponent) {
		g_InvalidInput = false;
		if (equal(base, 0.0) && exponent < 0) {
			g_InvalidInput = true;
			return 0.0;
		}

		unsigned int absExponent = (unsigned int)(exponent);
		if (exponent < 0)
			absExponent = (unsigned int)(-exponent);

		double result = PowerWithUnsignedExponent(base, absExponent);
		if (exponent < 0)
			result = 1.0 / result;
	}

	double PowerWithUnsignedExponent(double base, unsigned int exponent) {
		if (exponent == 0)
			return 1;
		if (exponent == 1)
			return base;

		double result = PowerWithUnsignedExponent(base, exponent >> 1);
		result *= result;

		if (exponent & 0x1 == 1)
			result *= base;

		return result;
	}
private:
	int g_InvalidInput = false;
};

좋은 웹페이지 즐겨찾기