Given a number x and an integer n, return x raised to the power n, that is, x multiplied by itself n times.
A negative power means one over the positive power: x⁻ⁿ = 1 / xⁿ. Any x to the power 0 is 1.
n can be as large as about two billion, so multiplying x in one step at a time is too slow. Aim for O(log |n|) multiplications.
Example 1
Input: x = 3.00000, n = 5
Output: 243.00000
3 × 3 × 3 × 3 × 3 = 243.
Example 2
Input: x = -1.50000, n = 3
Output: -3.37500
An odd power of a negative number stays negative.
Example 3
Input: x = 4.00000, n = -2
Output: 0.06250
4⁻² = 1 / 4² = 1 / 16 = 0.0625.
Constraints
-100.0 < x < 100.0, with at most five digits after the decimal point-2³¹ <= n <= 2³¹ - 1x is not 0 when n <= 0.-10⁴ <= xⁿ <= 10⁴10⁻⁵ of the true value, absolutely or relative to its size, is accepted.xⁿ = (x²)^(n/2) when n is even, and one extra x when it is odd. Each step halves the exponent, so about 31 squarings reach any n. A negative n is the same as raising 1/x to -n.
3.00000
243.00000