Boost logo

Boost :

From: Bruno Lalande (bruno.lalande_at_[hidden])
Date: 2008-02-04 07:44:32


>
> template <int N>
> struct positive_power
> {
> template <typename T>
> static float result(T base)
> {
> return (N%2) ? positive_power<1>::result(base)
> * positive_power<N-1>::result(base)
> : positive_power<2>::result(
> positive_power<N/2>::result(base)
> );
> }
> };
>
> I have no idea whether that would work better or not.

Basically, it gives the same performance without the need of a
specialization with 3. But this time you have to specialize with 2 if you
don't want your code to enter into an infinite loop in that case
(power<2>(n) = power<2>(power<1>(n)) = power<2>(power<1>(power<1>(n))),
etc....). My tests show that your solution is better for some exponents and
less good for some others, so I don't really know which one to choose. I
will do the same analysis on a few other platforms to make a decision.

I'd also
> wonder whether -ffast-math or similar flags make a difference.

Nope, there's no difference with this additional flag on my computer.

Thanks
Bruno


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk