Boost logo

Boost Users :

Subject: Re: [Boost-users] find the largest/most precise of two types
From: Barend Gehrels (barend_at_[hidden])
Date: 2009-05-20 17:32:33


John Maddock wrote:
>> I have a bunch of functions that accept two numeric arguments. In the
>> case that the types of these arguments are different I want to figure
>> out what the return type should be. The rule I would like to use is
>> that the largest of the two types should be used for internal
>> computations and the return type.
>>
>> f(someFloat, someDouble) -> double
>> f(someFloat, someFloat) -> float
>> f(someInt, someFloat) -> float
>>
>> Is there a way I can do this, perhaps with boost's type_traits?
>
> Take a look at
>
> boost::math::tools::promote_args<....>::type
>
> defined in boost/math/tools/promotion.hpp: it's an implementation
> detail so you probably shouldn't rely upon it, but it should give you
> a good head start. Note that it treats all integer arguments "as if"
> they were really doubles - so not quite what you asked for - this was
> done for compatibility with the rules in C99 and the upcoming C++
> standard that integer arguments to math functions should be treated as
> doubles. Should be easy to adapt to what you want though,
>
> HTH, John.
>
The promote_args are fine for many cases, Steven's solutions looks also
interesting.

Last year we needed also these things for GGL, couldn't find them in
Boost, and created a (maybe temporary) alternative solution, see here
http://tinyurl.com/q26e8w

Difference with promote_args is that here two integer types stay integer
(in promote_args they go to double, only two floats stay float). So it
depends what you want to do with them, but int/float -> float is done here.

It has to be extended though (e.g. for long doubles) c.q. implementation
might be enhanced using some meta-code.

Regards, Barend


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net