
Boost : 
Subject: Re: [boost] returning floating point values from a metafunction
From: Christopher Kormanyos (e_float_at_[hidden])
Date: 20110701 17:21:36
... Just for fun, I was trying to write a compiletime approximation of cosine using the following Maclaurin series:
cos( x ) ~= 1  ( x^2 / 2! ) + ( x^4 / 4! )  ( x^6 / 6! ) + ...
I ended up at a point where I needed to assign a const floating point value to the result of an integer division, kind of like this:
static const float value = integerA / integerB;

Sometimes, I use Pade approximations for this kind of thing. But, as already mentioned,
you also need to consider rational approximations to floats when metaprogramming.
Consider the Pade approximation for sin(x) of Order(3) in the numerator and
Order(4) in the denominator:
sin(x) ~= (5880 x  620 x^3)/(5880 + 360 x^2 + 11 x^4)
I believe that it is easier to rationalize and metaprogram this thing than it is to work
with a similarly truncated Taylor series. In fact, it's probably a viable alternative to
your existing macros.
I obtain Pade approximations using Mathematica. However, Wolfram's
magnificent knowledgebased search machine "Alpha" will actually give you
Pade approximations.
For example, put this into Wolfram's Alpha:
PadeApproximant[Sin[x], {x, 0, {3, 4}}]
Of course this stuff also works for sin, exp, log, etc.
Maybe this will give you some new ideas or at least something to play around with.
Sincerely, Chris.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk