[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]
template<> struct NumericTraits<ArithmeticType> |
Unary traits for promotion, conversion, creation of arithmetic objects.
#include <vigra/numerictraits.hxx>
This traits class is used derive important properties of an arithmetic type. Consider the following algorithm:
The return type of this function can not be 'unsigned char' because the summation would very likely overflow. Since we know the source type, we can easily choose 'int' as an appropriate return type. Likewise, we would have chosen 'float' if we had to sum a sequence of floats. If we want to make this algorithm generic, we would like to derive the appropriate return type automatically. This can be done with NumericTraits. The code would look like this (we use Data Accessors to read the data from the sequence):
In this example NumericTraits is not only used to deduce the ReturnType of the operation, but also to initialize it with the constant 'zero'. This is necessary since we do not know in general, which expression must be used to obtain a zero of some arbitrary type - 'ResultType result = 0;
' would only work if the ResultType had an constructor taking an 'int
' argument, and we would not even have any guarantee as to what the semantics of this constructor are. In addition, the traits are used to cast the source type into the promote type.
Similarly, an algorithm that needs multiplication would use the return type RealPromote
and the functions one()
and toRealPromote()
. The following members are defined in NumericTraits<ArithmeticType>
:
typedef ... Type; | the type itself |
typedef ... Promote; | promote type for addition and subtraction |
typedef ... RealPromote; | promote type for multiplication and division with a real number (only defined if |
typedef ... ComplexPromote; | promote type for complex arithmetic |
typedef ... ValueType; | for scalar types: the type itself |
static Promote toPromote(ArithmeticType v); | convert to |
static RealPromote toRealPromote(ArithmeticType v); | convert to (only defined if |
static ArithmeticType fromPromote(Promote v); | convert from if |
static ArithmeticType fromRealPromote(RealPromote v); | convert from (only defined if if if |
static ArithmeticType zero(); | create neutral element of addition i.e. |
static ArithmeticType nonZero(); | create a non-zero element (if multiplication is defined, this yields one()) i.e. |
static ArithmeticType min(); | the smallest number representable in this type. |
static ArithmeticType max(); | the largest number representable in this type. |
static ArithmeticType one(); | create neutral element of multiplication (only defined if i.e. |
typedef ... isIntegral; | VigraTrueType if |
typedef ... isScalar; | VigraTrueType if |
typedef ... isSigned; | VigraTrueType if |
typedef ... isOrdered; | VigraTrueType if |
typedef ... isComplex; | VigraTrueType if |
NumericTraits for the built-in types are defined in #include <vigra/numerictraits.hxx>
Namespace: vigra
© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) |
html generated using doxygen and Python
|