[mpl] compile time integer power
Hi All,
This is a newbie mpl question, and here goes:
How can evaluate integer powers using mpl? Such as,
template < unsigned Pow, class Real > Real pow( Real r )
{
return
::type number; BOOST_STATIC_ASSERT( number::value == -3*SIZE );
Thanks a lot
On 1/22/07, Sami
Hi All,
This is a newbie mpl question, and here goes:
How can evaluate integer powers using mpl? Such as,
template < unsigned Pow, class Real > Real pow( Real r ) { return
; // the expression is compile time, // not the result of course.
if (Pow == 0) return 1; // to do argue about the value of '0 to the 0'.
if (Pow == 1) return r;
return pow
Now, why do you want it - I doubt that it will buy you anything in terms of performance. Tony
Hi Tony,
Thx for your reply first of all.
On 1/22/07, Gottlob Frege
On 1/22/07, Sami
wrote: Hi All,
if (Pow == 0) return 1; // to do argue about the value of '0 to the 0'. if (Pow == 1) return r; return pow
(r) * r; }
Now, why do you want it - I doubt that it will buy you anything in terms of performance.
The issue is not particularly about performance, it is about being able to do this at compile time; other than, well, trying out MPL. Your resolution to the problem is quite nice, though that's not what I am asking. My question is about MPL, not "how to do this with C++"... There should be a way of doing this using MPL with a single line of code. Or, even compute integer power of integers and get a compile time result. This is useful for many metaprograms and again, not a performance concern. Any ideas?
On 1/22/07, Sami
Hi Tony,
Thx for your reply first of all.
On 1/22/07, Gottlob Frege
wrote: On 1/22/07, Sami
wrote: Hi All,
if (Pow == 0) return 1; // to do argue about the value of '0 to the 0'. if (Pow == 1) return r; return pow
(r) * r; }
Now, why do you want it - I doubt that it will buy you anything in terms of performance. The issue is not particularly about performance, it is about being able to do this at compile time; other than, well, trying out MPL. Your resolution to the problem is quite nice, though that's not what I am asking. My question is about MPL, not "how to do this with C++"...
There should be a way of doing this using MPL with a single line of code. Or, even compute integer power of integers and get a compile time result. This is useful for many metaprograms and again, not a performance concern.
Any ideas?
One more note. In my original post, I mentioned this constant sequence wrapper. If there were such a thing (is there?) then one could evaluate integer power of an integer at compile time. For instance: const unsigned SIZE = 3; const unsigned EIGHT = fold ( constant_sequence< int_<2>, SIZE >, int_<1>, times< _ , _ > :: type :: value ; This is only a thought. Maybe there is a better way of computing powers, without using fold or that constant_sequence thingy.. As for the real powers, maybe there is a Boost.Fusion algorithm that can do it.. sami.
--- Sami wrote:
On 1/22/07, Sami wrote:
Hi Tony,
Thx for your reply first of all.
On 1/22/07, Gottlob Frege wrote:
On 1/22/07, Sami wrote:
Hi All,
if (Pow == 0) return 1; // to do argue about
0'. if (Pow == 1) return r; return pow
(r) * r; }
Now, why do you want it - I doubt that it will buy you anything in terms of performance. The issue is not particularly about performance, it is about being able to do this at compile time; other than, well,
resolution to the problem is quite nice, though
the value of '0 to the trying out MPL. Your that's not what I am asking.
My question is about MPL, not "how to do this with C++"...
There should be a way of doing this using MPL with a single line of code. Or, even compute integer power of integers and get a compile time result. This is useful for many metaprograms and again, not a performance concern.
Any ideas?
One more note. In my original post, I mentioned this constant sequence wrapper. If there were such a thing (is there?) then one could evaluate integer power of an integer at compile time. For instance:
const unsigned SIZE = 3; const unsigned EIGHT = fold ( constant_sequence< int_<2>, SIZE >, int_<1>, times< _ , _ > :: type :: value ;
This is only a thought. Maybe there is a better way of computing powers, without using fold or that constant_sequence thingy..
As for the real powers, maybe there is a Boost.Fusion algorithm that can do it..
Actually, there are two MPL algorithms in the Boost Vault < http://boost-consulting.com/vault/ >. Look inside the Template Metaprogramming folder for mpl_math.zip; the algorithms are integral_power and power, IIRC. Cromwell D. Enage ____________________________________________________________________________________ We won't tell. Get more on shows you hate to love (and love to hate): Yahoo! TV's Guilty Pleasures list. http://tv.yahoo.com/collections/265
On 1/23/07, Cromwell Enage
--- Sami wrote:
On 1/22/07, Sami wrote:
Hi Tony,
Thx for your reply first of all.
On 1/22/07, Gottlob Frege wrote:
On 1/22/07, Sami wrote:
Hi All,
if (Pow == 0) return 1; // to do argue about
0'. if (Pow == 1) return r; return pow
(r) * r; }
Now, why do you want it - I doubt that it will buy you anything in terms of performance. The issue is not particularly about performance, it is about being able to do this at compile time; other than, well,
resolution to the problem is quite nice, though
the value of '0 to the trying out MPL. Your that's not what I am asking.
My question is about MPL, not "how to do this with C++"...
There should be a way of doing this using MPL with a single line of code. Or, even compute integer power of integers and get a compile time result. This is useful for many metaprograms and again, not a performance concern.
Any ideas?
One more note. In my original post, I mentioned this constant sequence wrapper. If there were such a thing (is there?) then one could evaluate integer power of an integer at compile time. For instance:
const unsigned SIZE = 3; const unsigned EIGHT = fold ( constant_sequence< int_<2>, SIZE >, int_<1>, times< _ , _ > :: type :: value ;
This is only a thought. Maybe there is a better way of computing powers, without using fold or that constant_sequence thingy..
As for the real powers, maybe there is a Boost.Fusion algorithm that can do it..
Actually, there are two MPL algorithms in the Boost Vault < http://boost-consulting.com/vault/ >. Look inside the Template Metaprogramming folder for mpl_math.zip; the algorithms are integral_power and power, IIRC.
Cromwell D. Enage
wouv! This library blew my head off. I was only asking for a silly single line statement, look what you have done in there! I am so surprised that this library is buried down in the vault and didn't make it to Boost collection yet.. thanks Cromwell. sami
--- Sami wrote:
wouv! This library blew my head off. I was only asking for a silly single line statement, look what you have done in there!
Peder Holt should get the majority of the credit for writing the compile-time double numeric type and the guts of the advanced math metafunctions; I just organized the framework and added some other numeric types.
I am so surprised that this library is buried down in the vault and didn't make it to Boost collection yet..
Well, the lack of user-friendly documentation is the biggest showstopper so far. (The inability to compile the euler.cpp example program even with a Pentium 4 and 512 MB of RAM comes in at a close second.)
thanks Cromwell.
No problem. Cromwell D. Enage ____________________________________________________________________________________ The fish are biting. Get more visitors on your site using Yahoo! Search Marketing. http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php
participants (3)
-
Cromwell Enage
-
Gottlob Frege
-
Sami