Boost logo

Boost :

Subject: Re: [boost] Introduction of numeric_adaptor
From: John Maddock (john_at_[hidden])
Date: 2009-11-03 05:06:06


>> what's wrong with lexical_cast?
>> it would put the requirement of operator<< on the number type T
>
> Actually my concern about using an operator<< was about performance,
> but on second thought there would be no performance hit doing like
> that, I think I was confusing with another use case of streaming
> operators. I will revise that, and will be happy to get back to
> lexical_cast if there's no problem.

Also take a look at the machinery we defined in
boost/math/tools/constants.hpp, usage is a little clunky, for example:

BOOST_DEFINE_MATH_CONSTANT(name, digits, extra-digits, exponent);

after which you can use

name<T>()

to refer to the constant. Built in types simply return an appropriately
defined numeric constant (so no lexical_cast overhead), while for UDT's the
constant is defined as a string containing which gets lexical_cast'ed to the
value.

The remaining issue we haven't sorted out is thread safety: for UDT's the
casted value is stored in a static variable for efficiency reasons, so only
the first call results in a lexical_cast, but of course this isn't strictly
thread safe (for built in types, there is no such issue).

The "extra-digits" argument is required BTW because many compilers reject
numeric constants with too many digits precision, so we put enough decimal
places in "digits" to ensure 128-bit long double precision, and everything
else (used only in extreme UDT cases) in "extra-digits".

John.


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