[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]
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:
// calculate the sum of a sequence of bytes int sumBytes(unsigned char * begin, unsigned char * end) { int result = 0; for(; begin != end; ++begin) result += *begin; return result; }
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 choosen '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):
// calculate the sum of any sequence template <class Iterator, class Accessor> typename vigra::NumericTraits<typename Accessor::value_type>::Promote sumSequence(Iterator begin, Iterator end, Accessor a) { // an abbreviation typedef vigra::NumericTraits<typename Accessor::value_type> SrcTraits; // find out result type typedef typename SrcTraits::Promote ResultType; // init result to zero ResultType result = vigra::NumericTraits<ResultType>::zero(); for(; begin != end; ++begin) { // cast current item to ResultType and add result += SrcTraits::toPromote(a(begin)); } return result; }
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>
:
|
the type itself |
|
promote type for addition and subtraction |
| promote type for multiplication and division with a real number (only defined if |
|
promote type for complex arithmetic |
|
for scalar types: the type itself |
| convert to |
| convert to (only defined if |
| convert from if |
| convert from (only defined if if if |
| create neutral element of addition i.e. |
| create a non-zero element (if multiplication is defined, this yields one()) i.e. |
| the smallest number representable in this type. |
| the largest number representable in this type. |
| create neutral element of multiplication (only defined if i.e. |
| VigraTrueType if |
| VigraTrueType if |
| VigraTrueType if |
| VigraTrueType if |
| 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
|