If n is negative, convert to positive by taking 1 / x and flipping n to -n. Then use fast exponentiation on the positive n. This works because x^(-n) = (1/x)^n.
Example: x = 2, n = -2. Convert to x = 0.5, n = 2. Then 0.5^2 = 0.25. Same as 1 / (2^2) = 1 / 4 = 0.25. The conversion keeps the logic simple.
Watch for edge cases: n = 0 returns 1 for any x. x = 0 with negative n is undefined (division by zero), but test cases usually avoid this. x = 1 always returns 1 regardless of n.