We’re preparing your current view and syncing the latest data.
Given a double x and an integer n, implement a function to calculate x raised to the power n (x^n). The function should handle negative powers and be efficient for large n.
Two inputs: a floating-point number x and an integer n.
A floating-point number representing x raised to the power n.
-100.0 < x < 100.0, -2^31 <= n <= 2^31 - 1
Example 1
Input
x = 2.0, n = 10
Output
1024.0
Explanation
2 raised to the power 10 is 1024
Example 2
Input
x = 2.1, n = 3
Output
9.261
Explanation
2.1^3 = 2.1 * 2.1 * 2.1 = 9.261
Example 3
Input
x = 2.0, n = -2
Output
0.25
Explanation
2 raised to the power -2 is 1 / (2^2) = 0.25