Boost logo

Boost Users :

Subject: Re: [Boost-users] find the largest/most precise of two types
From: John Maddock (john_at_[hidden])
Date: 2009-05-20 12:40:02

> 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


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.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at