returning floating point values from a metafunction

This isn't directly Boost-related, but this is still probably the best place to ask this question. Just for fun, I was trying to write a compile-time 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; My problem is of course that you can't cast to non integral/enum types in a constant expression, which means that I end up with the truncated result of integer division. Suggestions?

--- On Fri, 7/1/11, Kenny Riddile wrote:
If your code really needs to perform compile-time floating-point math, then I have two suggestions: 1. Use the recently accepted Boost.Ratio library (which should become part of the next release). 2. Try out the MPL.Math sublibrary (not yet accepted into Boost, currently located at <https://github.com/boost-vault/Template-Metaprogramming/blob/master/mpl_math.zip>) and browse the existing source code. Sorry, no real documentation yet. HTH, Cromwell D. Enage

On 7/1/2011 1:05 PM, Cromwell Enage wrote:
I was just trying it for fun. I already had a preprocessor macro solution, but wanted to see how feasible a pure TMP solution would be. This is fairly accurate though: http://pastebin.com/jz69vxYn

--- On Fri, 7/1/11, Kenny Riddile wrote:
I've found that a pure TMP solution would have quickly bogged down MSVC and GCC over even moderate amounts of compile-time trigonometric calculations. The proposed MPL.Math sublibrary mixes PP and TMP in an attempt to balance accuracy and minimal template instantiations. It has several working examples that should be enough at least to satisfy your curiosity. HTH, Cromwell D. Enage

On Fri, Jul 1, 2011 at 10:39 AM, Kenny Riddile <kfriddile@gmail.com> wrote: [...]
Just a quick suggestion: you can replace the x * x * ... * x's with boost::math::pow<n>(x): http://www.boost.org/libs/math/doc/sf_and_dist/html/math_toolkit/special/pow... - Jeff

... Just for fun, I was trying to write a compile-time 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 knowledge-based 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.
participants (4)
-
Christopher Kormanyos
-
Cromwell Enage
-
Jeffrey Lee Hellrung, Jr.
-
Kenny Riddile