[MPL] Compile-time math metafunctions.

Hi, all! I spent a bit of time reading Pete Isenesee's article, "Fast Math Using Template Metaprogramming", which can be found in Game Programming Gems, Section 1.2, and it inspired me to implement compile-time versions of some of the standard math functions. Two of the metafunctions even work for complex numbers, using the components as input instead of any particular complex number class. My work so far can be found here: <http://groups.yahoo.com/group/boost/files/mpl_math.zip> Yes, it also provides the math constant PI. Perhaps a constants library can be built on top of this one? I'm holding off on putting this mini-library in the Sandbox until the file hierarchy is reasonably stable; I'm not even sure if the classes are well-structured at this point. For this reason, I'm also holding off on writing documentation; I'm hoping the example program would be simple enough to understand. Please send feedback! Cromwell Enage __________________________________ Do you Yahoo!? SBC Yahoo! - Internet access at a great low price. http://promo.yahoo.com/sbc/

Cromwell Enage <sponage@yahoo.com> writes:
Hi, all!
I spent a bit of time reading Pete Isenesee's article, "Fast Math Using Template Metaprogramming", which can be found in Game Programming Gems, Section 1.2, and it inspired me to implement compile-time versions of some of the standard math functions. Two of the metafunctions even work for complex numbers, using the components as input instead of any particular complex number class. My work so far can be found here:
<http://groups.yahoo.com/group/boost/files/mpl_math.zip>
Yes, it also provides the math constant PI. Perhaps a constants library can be built on top of this one?
I'm holding off on putting this mini-library in the Sandbox until the file hierarchy is reasonably stable; I'm not even sure if the classes are well-structured at this point. For this reason, I'm also holding off on writing documentation; I'm hoping the example program would be simple enough to understand.
Please send feedback!
Did you compile it? If so, on what compilers? I can't see how it could possibly work. #include <boost/config.hpp> template <typename RealNum> struct sample_t { BOOST_STATIC_CONSTANT(RealNum, base = 5.0); BOOST_STATIC_CONSTANT(RealNum, power = 1.0); BOOST_STATIC_CONSTANT(RealNum, angle = 1.234); }; is an illegal program (try comeau online to see some reasons why). Also, it's not very mpl-ish. Nothing seems to follow the metafunction protocol of operating on and returning types. I hate to be discouraging; it's probably a really interesting idea. I am only looking as far as some details of the implementation, here. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

--- David Abrahams <dave@boost-consulting.com> wrote:
Did you compile it? If so, on what compilers?
My main development compiler is MinGW 3.2; I haven't actually tested it on MSVC 6.
I can't see how it could possibly work.
I just read that not all compilers support inline initialisation of member constants. Arrgh!
#include <boost/config.hpp>
template <typename RealNum> struct sample_t { BOOST_STATIC_CONSTANT(RealNum, base = 5.0); BOOST_STATIC_CONSTANT(RealNum, power = 1.0); BOOST_STATIC_CONSTANT(RealNum, angle = 1.234); };
is an illegal program (try comeau online to see some reasons why).
I was hoping BOOST_STATIC_CONSTANT would work on more than just integral types.
Also, it's not very mpl-ish. Nothing seems to follow the metafunction protocol of operating on and returning types.
Oh, okay. I'll keep this protocol in mind the next time I try to write MPL metafunctions. Right now, it seems I can't directly use templates anyway for this library.
I hate to be discouraging; it's probably a really interesting idea.
No problem. I have a need for this sort of thing myself, so I'll definitely keep working at it.
I am only looking as far as some details of the implementation, here.
I'll look into Boost.Preprocessor to see if I can implement my functions via macros. I'll send another email when I have results. Cromwell Enage __________________________________ Do you Yahoo!? SBC Yahoo! - Internet access at a great low price. http://promo.yahoo.com/sbc/

David Abrahams wrote:
is an illegal program (try comeau online to see some reasons why).
Also, it's not very mpl-ish. Nothing seems to follow the metafunction protocol of operating on and returning types.
I hate to be discouraging; it's probably a really interesting idea. I am only looking as far as some details of the implementation, here.
What happened to fixed_c? It's used here http://www.boost.org/libs/mpl/doc/paper/html/codegeneration.html in a form mpl::fixed_c<9,0001> (he-he). Any plans to redesign it and to put into MPL? -- Alexander Nasonov

Alexander Nasonov writes:
David Abrahams wrote:
is an illegal program (try comeau online to see some reasons why).
Also, it's not very mpl-ish. Nothing seems to follow the metafunction protocol of operating on and returning types.
I hate to be discouraging; it's probably a really interesting idea. I am only looking as far as some details of the implementation, here.
What happened to fixed_c?
Nothing; the stub is still there, waiting for somebody to implement it in full.
It's used here http://www.boost.org/libs/mpl/doc/paper/html/codegeneration.html in a form mpl::fixed_c<9,0001> (he-he). ^^^^
This one is actually kind of nasty -- ignoring the fact that 0001 is an octal constant, there is no way you can detect the number of leading zeros here, which renders this particular representation unusable. I guess we can go with something like float_<0,90001,e<+1> > or something along these lines.
Any plans to redesign it and to put into MPL?
Well, somebody who needs it needs to tackle it. It would be a welcome contribution. -- Aleksey Gurtovoy MetaCommunications Engineering

--- Aleksey Gurtovoy <agurtovoy@meta-comm.com> wrote:
Alexander Nasonov writes:
What happened to fixed_c?
Nothing; the stub is still there, waiting for somebody to implement it in full.
It's used here
http://www.boost.org/libs/mpl/doc/paper/html/codegeneration.html
in a form mpl::fixed_c<9,0001> (he-he). ^^^^
It took me some time to realize that the floating-point number was in European notation. Slick.
This one is actually kind of nasty -- ignoring the fact that 0001 is an octal constant, there is no way you can detect the number of leading zeros here, which renders this particular representation unusable. I guess we can go with something like
float_<0,90001,e<+1> >
or something along these lines.
Any plans to redesign it and to put into MPL?
Well, somebody who needs it needs to tackle it. It would be a welcome contribution.
Since I'm the guy who started this entire thread, I guess I should look into it. Cromwell Enage __________________________________ Do you Yahoo!? SBC Yahoo! - Internet access at a great low price. http://promo.yahoo.com/sbc/
participants (4)
-
Aleksey Gurtovoy
-
Alexander Nasonov
-
Cromwell Enage
-
David Abrahams