$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] returning floating point values from a metafunction
From: Cromwell Enage (sponage_at_[hidden])
Date: 2011-07-01 13:05:12
--- On Fri, 7/1/11, Kenny Riddile wrote:
> 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?
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