Pow(x, n)
Medium · math
Given a decimal number x and a whole number n, return x raised to the power n: the result of multiplying x by itself n times.
Two special cases. When n is 0 the answer is 1, even if x is 0. When n is negative, the answer is 1 divided by x raised to the power of -n; for example 2 to the power -3 is 1 / 8 = 0.125.
Decimal arithmetic is never exact, so round your answer to 3 decimal places before returning it (a value exactly halfway between two candidates rounds up). Your answer is compared with the expected value exactly.
Examples
Example 1
Input: x = 2.1, n = 3
Output: 9.261
2.1 * 2.1 * 2.1 = 9.261.
Example 2
Input: x = 2.0, n = -2
Output: 0.25
2 to the power 2 is 4, and a negative exponent means 1 / 4 = 0.25.
Example 3
Input: x = 5.0, n = 0
Output: 1.0
Any number to the power 0 is 1.
Constraints
- -10.0 <= x <= 10.0
- -10^5 <= n <= 10^5
- If n is negative, x is not 0.
- The exact value of x to the power n is between -10^4 and 10^4.