Optimizing pow()ΒΆ

The implementation of x^a can be optimized for constant a.

It is obvious that for constant integer exponent you can just expand:

x^3 = x^1 \cdot x^2

x^9 = x^1 \cdot x^8

and since 13_{10} = 1101_2 = 2^0 + 2^2 + 2^3:

x^{13} = x^1 \cdot x^4 \cdot x^8

Likewise for fractional a:

x^\frac{3}{4} = x^\frac{1}{2} \cdot x^\frac{1}{4}

So for a = 2.4_{10} \simeq 10.011001101_2:

x^{2.4} \simeq x^2 \cdot x^\frac{1}{4} \cdot x^\frac{1}{8} \cdot x^\frac{1}{64} \cdot x^\frac{1}{128} \cdot x^\frac{1}{512}

This Page