Boost logo

Boost :

Subject: [boost] Introduction of numeric_adaptor
From: Bruno Lalande (bruno.lalande_at_[hidden])
Date: 2009-11-01 16:16:12


I would like to introduce the numeric_adaptor library that Barend and
I have developed to address a recurring problem: integration of big
numbers. We already talked about it in the past, when discussions
about geometry would lead to robustness. We're introducing it now
because of the upcoming formal review of GGL.

The purpose of this library is to turn big-numbers proposed by the
most well-known big number libraries into value-types, (almost) usable
like any number. This way, a library developer who has a robustness
sensible function to write can take numbers as template parameters,
and use them in such a way that they can be a fundamental type or one
of the value-types provided by numeric_adaptor.

This is basically done by:
- defining in all the big-number types all the operators needed to
manipulate them like fundamental types
- defining some free functions in the boost namespace for other
operations (sin, cos, hypot, conversions...), which have an default
overload for fundamental types and a specific overload for each
big-number type proposed


template <class T>
T my_robustness_sensitive_function(T value)
    value /= 2;
    T value2 = boost::to<T>("123.0123456789");
    value += boost::sqrt(value2);
    return value;

This function is numeric_adaptor-ready. If T is a fundamental type,
boost::to() will forward to boost::lexical_cast and boost::sqrt() will
forward to std::sqrt(). If T is one of the big number types provided
by the library, specific overloads will be used. The precision of the
function will thus entirely depend on the type passed. If the user
chooses to sacrifice precision and use fundamental types, there won't
be any run-time overhead. If the user wants something more precise and
uses a big-number type, the precision of the result will be the one of
the chosen type.

The library is extensible. Anyone can add a new big number type with
all needed operators, and overload the free functions (or specialize a
dedicated traits class - see boost::to()).

Note: For conversions, we would have liked to extend
boost::lexical_cast directly, but the library is not designed in that
sense. That's why we wrapped it into a boost::to() function which is
explicitly designed to be extended, via traits. Last week on this list
list a generic convert_to was discussed and that system could be used

The library is in the Boost sandbox:

The library currently proposes 2 big number types: CLN and GMP.

This is not a formal review request, rather a preview and a reference
that we will possibly use during the review of GGL. However, we would
really like to have your feedback and to know if such a library would
make sense.


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