Implement exponentiation. Given a real base x and an integer exponent n, return x raised to the power n, written x^n.
The exponent can be zero or negative. A negative exponent means the reciprocal of the corresponding positive power, so x^(-k) is 1 divided by x^k.
Example 1
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2
Input: x = 2.00000, n = -2
Output: 0.25000
2^(-2) = 1 / 2^2 = 1 / 4 = 0.25.
Constraints
−100.0 < x < 100.0−2^31 ≤ n ≤ 2^31 − 1 and n is an integerEither x is non-zero or n is greater than 0The final result stays within the range [−10^4, 10^4]See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI