/50. Power Function

50. Power Function

Medium
Probability38.3% acceptance

Given a floating-point base value base and an integer exponent exponent, compute base raised to the power exponent (base^exponent). Return the result as a floating-point number. Negative exponents should be handled by returning the reciprocal of the positive exponentiation. The function should not use built-in power functions.

Example 1

Input: base=3.0, exponent=4

Output: 81.0

Explanation: 3^4 = 81

Example 2

Input: base=0.5, exponent=6

Output: 0.015625

Explanation: 0.5^6 = 0.015625

Example 3

Input: base=-2.0, exponent=3

Output: -8.0

Explanation: (-2)^3 = -8

Example 4

Input: base=5.0, exponent=-1

Output: 0.2

Explanation: 5^-1 = 1/5 = 0.2

Constraints

  • -100.0 < base < 100.0
  • -231 <= exponent <= 231 - 1
  • exponent is an integer
  • Either base != 0 or exponent > 0
  • -104 <= base**exponent <= 104
Python (current runtime)

Case 1

Input: base=1.5, exponent=5

Expected: 7.59375

Case 2

Input: base=-3.0, exponent=2

Expected: 9.0

Case 3

Input: base=4.0, exponent=0

Expected: 1.0

Case 4

Input: base=0.25, exponent=2

Expected: 0.0625

Case 5

Input: base=10.0, exponent=-2

Expected: 0.01