Boost logo

Boost :

Subject: [boost] [mpl-cf] [RFC] MPL Real Numbers using Continued Fractions
From: Hal Finkel (half_at_[hidden])
Date: 2010-11-28 16:30:33


A few months ago, I created an MPL extension type for representing real
numbers. The indented use is for compile-time generation of
floating-point constants, although it could also be used for MPL-based
graph algorithms, etc. The primary design requirement was that almost
any compiler would be able to completely inline the conversion to
floating-point (which means no functional recursion can be used), and I
think I've achieved that. The current implementation uses a modified
version of MPL's vector type (modified to add the conversion to floating
point, add "overloads", and to remove the typeof-based implementation
variant (which is incompatible with the floating-point conversion)).

I've posted the code to the Boost vault under the "Template
Metaprogramming" section; the file name is

Continued fractions were chosen for the representation because the
associated algorithms are simple, and simple proved important for
keeping down compilation times. Even so, the compilation times might be
too long for practical use. You can decrease the compilation time by
using a shorter maximum cf-length by defining BOOST_MPL_LIMIT_CF_SIZE to
be 10 instead of 20. The current default is 20 to allow an effective
precision near that of a double. Also, continued fractions, being "based
on" rational numbers, can exactly represent any rational (for example,
1/3) so the computations can be more accurate than those using a
floating-point representation.

If you don't know what a continued fraction is, and would like to know,
you can check out the wikipedia page:

The current implementation supports addition, subtraction,
multiplication and division, comparison ops, some constants like pi, and
I've included sin()/cos() as well. I've not yet written any real
documentation, but the archive has a couple of test programs; the usage
is pretty simple. Each test program has many individual tests, so its
compilation time will be representative of a more-complicated
computation, if you just need to do a few things, it would be much

If there is interest, I'll add support for generating numbers using
metafunctions, write some documentation, jam files, etc. Let me know
what you think, and I welcome suggestions, criticisms, etc. Also, I've
currently tested using only the GNU and Intel compilers, so let me know
whether it does or does not work when using something else.

Thanks again,

P.S. I know that there is a mpl_math archive on the vault from back in
2007. I did not discover this until after I had almost finished writing
my mpl-cf implementation, but, the mailing-list messages from the time
indicate that it was too slow to be used practically, since I think the
continued fractions algorithms are simpler, mpl-cf might be better, but
I've not tested it to find out.

Boost list run by bdawes at, gregod at, cpdaniel at, john at