find the largest/most precise of two types
Hi there, 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? Cheers, Alex
AMDG Alex Flint 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?
A quick glance through type_traits didn't show anything.
How does this work:
#include
Steven Watanabe wrote:
#include
template
struct promote { BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, (T0() + T1())) typedef typename nested::type type; };
This is actually one of the most easy way to do it without tons of specialization. It also can be scaled up to N args type pretty easily. -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
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.
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
participants (5)
-
Alex Flint
-
Barend Gehrels
-
Joel Falcou
-
John Maddock
-
Steven Watanabe