Sword refers to an offer face question 11 The integer number of the value of the sword point

Source: Internet
Author: User
Tags error handling
face question 11: The integer number of the value of the second party

Implement the function double power (double base,int exponent) to find the exponent of base, do not use the library function, do not consider the problem of large numbers.

Note: It is not possible to determine whether two decimals are equal or not, because the computer indicates a decimal error, and we can only judge whether the absolute value of their difference is within a very small range.

Digression: Three ways to process error handling: Return value, global variable, exception. The return value benefit is based on the return value to determine the cause of the error, the disadvantage is that the function can only return a value, the use of inconvenience. The global variable does not occupy the result of the function return. The exception is clearer, but some languages are not supported.

The Java implementation of the general idea is as follows:

public class Power {
	private double power (double base,int exponent) throws exception{
		double result = 0.0;
		if (equal (base,0.0) && exponent<0) {
			throw new Exception ("negative power of 0 is meaningless");
		}
		if (exponent = 0) {//0 of the 0 parties have no meaning, returns 0 or 1 can return
			1.0;
		}
		if (Exponent > 0) {result
			= Powerwithexponent (base, exponent);
		} else{//negative number power result
			= Powerwithexponent (1.0/base,-exponent);
		}
		return result;
	}
	Computed results
	private double powerwithexponent (double base,int exponent) {
		double result = 1.0;
		for (int i=1;i<=exponent;i++) {result
			= result * BASE;
		}
		return result;
	}
	Determines whether two decimals are equal
	private Boolean equal (double num1,double num2) {if ((
		num1-num2) > -0.0000001) && ( NUM1-NUM2) < 0.0000001) {return
			true;
		} else return false;
	public static void Main (string[] args) throws Exception {power
		test = new Power ();
		System.out.println (Test.power (2, 3));
	}

But the above procedure is not the most efficient calculation process, because if you ask for 32 times, you have to cycle 31 times, but if we've got 16 squares, we'll just have to square them again, and 16 can be squared by 8, and so on.

When n is an even number, it is concluded that a^n=a^ (N/2) *a^ (N/2), n is odd, a^n=a^ ((n-1)/2) *a^ ((n-1)/2) *a, the calculation is reduced, can be implemented recursively, the complexity from O (n) into O (Logn).

Details: Using the right shift operator instead of dividing by 2, use the operator instead of the remainder to judge the parity, because the bitwise operator is much more efficient. The procedure is as follows:

Private double PowerWithExponent2 (double base,int exponent) {
	if (exponent = = 0) return 1;
	if (exponent = = 1) return base;//Two recursive termination conditions double result
	= PowerWithExponent2 (base, exponent >> 1);//move 1 digits to the right to replace 2 C3/>result *= result;
	if ((exponent & 1) = = 1) result *= base;//binary minimum bit is 1, then the odd return result
	;
	Take the input (2,6) as an example to illustrate: After the start, enter the first recursion, enter (2,3)
	//Then enter the second recursion, enter as (2,1), at which time the exponent is 1, and the base is 2, which means that the second recursive execution result is 2
	// And then back to the first recursion, where the result is the second recursive results of 2, and then down to execute, resulting is 2*2, the first recursive exponent as 3,result=4*2, return 8
	//So the first recursive execution result is 8, then return to the original function , the result in it is 8 and then the 8*8 is executed, at which point the exponent is 6, and the final outcome is 64.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.