Boost logo

Boost :

Subject: Re: [boost] returning floating point values from a metafunction
From: Kenny Riddile (kfriddile_at_[hidden])
Date: 2011-07-01 13:39:32


On 7/1/2011 1:05 PM, Cromwell Enage wrote:
> --- 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
>
> _______________________________________________
> Unsubscribe& other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk