Boost logo

Boost :

From: Martin Bonner (Martin.Bonner_at_[hidden])
Date: 2008-02-06 06:00:50


From: John Maddock

> Martin Bonner wrote:
>>> The only use case I can think of is where the argument is an integer
>>> rather than a real number (and the result is therefore a double),
>>
>> boost.units:
>> pow<3>( length )
>> is going to have units of volume.
>
> Ah yes, forgot that. My gut feeling is that Boost.Math should
> concentrate on the um... math: provide a nice simple
> implementation that's reasonably efficient too. And libraries
> like Boost.Units can overload it for dimentioned-quantities, and
> use the Boost.Math version for the internal calculation.
> Does this make sense?
In principle yes, but in practise, I think Boost.Units will need to
calculate the result type via the * operator, so it might as well
calculate the result value at the same time. Of course, that doesn't
mean Boost.Units can't copy the implementation mechanism but then the
advantage of having "pow" in a single location is lost.

If double + Boost.Units can be supported without too much complexity,
then I think it should be. Otherwise, just handle double.
>
>> ... and I /believe/ that boost.units only needs half powers. So
>> (modulu units)
>>
>> template<class T1, class T2, int N>
>> T2 pow<N,2>(T1 val) { return sqrt(pow<N>(val)); }
>>
>> should do it.
>>
>> ... in fact, I suspect that boost.math pow should /only/ offer the
>> integral powers, and boost.units should wrap it to add the rational
>> support.
>
> I guess the question is can we evaluate half-powers more efficiently
> than just using pow? Maybe, but it's not so obvious to me as it is
for
> smallish integer powers.
I would expect that a general purpose pow would internally involve a
logarithm, a multiplication, and an exponentiation (not necessarily to
base e). My gut reaction is that will be slower than a few
multiplications and a sqrt (but gut reaction is a pessimal way of
optimizing).

-- 
Martin Bonner
Senior Software Engineer/Team Leader
PI SHURLOK LTD
Telephone: +44 1223 441434 / 203894 (direct)
Fax: +44 1223 203999
Email: martin.bonner_at_[hidden]
www.pi-shurlok.com
disclaimer

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