Let's trace pow(3.0, 5).
is odd. Pull out one : result , now compute .
is even. Square the base: , .
is even. Square again: , .
is odd. Multiply into result: . That's our answer.
You halve at each step, so the loop runs times. Each step does constant work. That's time, space.