Skip to content

Pow(x, n)

MediumBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

Compute x raised to the power n, where x is a floating-point number and n is an integer that may be negative. Do not use the built-in power operator or Math.pow. Answers within 10^-5 are accepted.

x = 2.0, n = 10   →  1024.0
x = 2.1, n = 3    →  9.261
x = 2.0, n = -2   →  0.25    (1 / 2^2)

Constraints

  • -100.0 < x < 100.0
  • -2^31 <= n <= 2^31 - 1
  • Either x is not zero or n > 0.
  • -10^4 <= x^n <= 10^4