casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Static Public Member Functions | Static Public Attributes | List of all members
casacore::NumericTraits< T > Class Template Reference

Relationships between numeric data types. More...

#include <NumericTraits.h>

Public Types

typedef T value_type
 Template argument. More...
 
typedef Char BaseType
 Numeric type. More...
 
typedef Char ConjugateType
 Conjugate (real<->complex) type. More...
 
typedef Char PrecisionType
 Higher precision type (Float->Double) More...
 

Static Public Member Functions

static uInt size ()
 Number of relevant numeric values. More...
 
static void setImag (T &, const BaseType &)
 Set the imaginary part of a complex value only (a NOP for reals) More...
 
static BaseType getValue (const T &, const uInt)
 Get the nsize()-th numeric value. More...
 
static void setValue (T &, const BaseType &, const uInt)
 Set the nsize()-th numeric value. More...
 

Static Public Attributes

static const Doubleepsilon
 Relevant minimum and maximum numbers. More...
 
static const Doubleminimum
 
static const Doublemaximum
 

Detailed Description

template<class T>
class casacore::NumericTraits< T >

Relationships between numeric data types.

Intended use:

Public interface

Review Status

Reviewed By:
nkilleen
Date Reviewed:
1996/12/12
Test programs:
tConvolver,tFFTServer

Etymology

A trait is a characteristic feature. NumericTraits defines relationships between and characteristics of Numeric data types.

Synopsis

This templated class contains a number of typedefs and definitions that describe the relationship between different numeric data types and there characteristics. Its use is in templated classes either where the use of one type implictly implies the use of a corresponding one or where a characteristic value differs with templating argument. Use of this class often avoids the need for double templating.

Currently this class defines the following relationships:

value_type
The template type itself. The name value_type is the C++ standard (e.g. DComplex::value_type equals double)
BaseType
The numeric base type. I.e. Double for Double and DComplex; Float for Float and Complex
ConjugateType
The corresponding complex type for a real type, and real type for a complex type. It is the type of the result if a Fourier Transform was to be done.
PrecisionType
The Type of the next higher numerical precision. I.e. Double or DComplex

And the following characteristics:

epsilon
A Double containing the smallest value such that 1+epsilon is different from 1.
minimum
A Double containing the smallest positive representable number, excluding denormalised numbers.
maximum
A Double containing the largest representable number.
size()
The number of numeric values in the templated entity. It will be 2 for complex numbers; 1 for real numbers.
setImag(T &other, const BaseType &val)
Set an imaginary part (for complex numbers) or a NOP (for reals).
getValue(T &other, const uInt n)
Get the nsize()-th value in the argument. For complex numbers the sequence is real, imaginary part.
setValue(T &other, const BaseType &val, const uInt n)
Set the nsize()-th value in the argument. For complex numbers the sequence is real, imaginary part.

For complex numbers these values are applicable to the real or imaginary components separately.

The use of this class is best illustrated in a number of examples.

A default template declaration is required by the C++ standard. It should never be used, except through the specialisations. The default types for ConjugateType and PrecisionType are deliberatly set to a non-numeric type to further discourage the use of the non-specialized class defined below. It also helps when using this class with the Sun native compiler.
Warning: The specialized instantiations seem to have a name with an appended code; This is only for cxx2html reasons; The name is in all cases NumericTraits

This class is implemented as a number of specialisations for the following data types.

This class should not be used with other template types and does nothing except return its template type if it is used. ie.
NumericTraits<ArbitraryType>::ConjugateType returns Char and
NumericTraits<ArbitraryType>::PrecisionType returns Char
NumericTraits<ArbitraryType>::epsilon is undefined
NumericTraits<ArbitraryType>::minimum is undefined
NumericTraits<ArbitraryType>::maximum is undefined

Example

Example 1:

Suppose you are writing a templated class that needs to do Fourier Transforms. The FFTServer class can do FFT's of Float or Double data types, but you need to doubly template it on the conjugate data type. To avoid having the conjugate data type appear as a template in the class you are writing you can use the ConjugateType typedef.

template<class T> class myClass {
private:
FFTServer<T, NumericTraits<T>::ConjugateType> server;
}

The ConjugateType transforms

Example 2:

Suppose you have a templated numerical integrator class. Because the individual samples can be negative it is possible to add two numbers of nearly equal magnitude but opposite sign and lose precision considerably. One way to combat this is to make the accumulator variable the next higher precision numerical type. The PrecisionType typedef defines what type this is

template<class T> class Integrator {
private:
NumericTraits<T>::PrecisionType accumulator;
}

The PrecisionType transforms

Example 3:

Suppose you have a templated class that needs to use the allNear functions from ArrayMath to determine if a templated Array is near one. The tolerance argument to the allNear function will depend on the template type and this is not known until the template is instantiated. The epsilon trait can be used to supply this value.

template<class T> void myClass<T>::myFunction(Array<T> & aArray) {
if (allNear(aArray, T(1), NumericTraits<T>::epsilon))
return;
// Do something
}
NumericTraits<T>::epsilon
is FLT_EPSILON for Float and Complex types and DBL_EPSILON for Double and DComplex data types.
NumericTraits<T>::minimum
is FLT_MIN for Float and complex Types and DBL_MIN for Double and DComplex data types.
NumericTraits<T>::maximum
is FLT_MAX for Float and complex Types and DBL_MAX for Double and DComplex data types.

See the C class/namespace for the values of these variables.

Motivation

This is a nice way to make the Convolver class singly templated (as it should be), even though the FFTServer it contains is doubly templated.

Thrown Exceptions

To Do

Definition at line 223 of file NumericTraits.h.

Member Typedef Documentation

template<class T>
typedef Char casacore::NumericTraits< T >::BaseType

Numeric type.

Definition at line 228 of file NumericTraits.h.

template<class T>
typedef Char casacore::NumericTraits< T >::ConjugateType

Conjugate (real<->complex) type.

Definition at line 230 of file NumericTraits.h.

template<class T>
typedef Char casacore::NumericTraits< T >::PrecisionType

Higher precision type (Float->Double)

Definition at line 232 of file NumericTraits.h.

template<class T>
typedef T casacore::NumericTraits< T >::value_type

Template argument.

Definition at line 226 of file NumericTraits.h.

Member Function Documentation

template<class T>
static BaseType casacore::NumericTraits< T >::getValue ( const T &  ,
const uInt   
)
inlinestatic

Get the nsize()-th numeric value.

Definition at line 244 of file NumericTraits.h.

template<class T>
static void casacore::NumericTraits< T >::setImag ( T &  ,
const BaseType  
)
inlinestatic

Set the imaginary part of a complex value only (a NOP for reals)

Definition at line 242 of file NumericTraits.h.

template<class T>
static void casacore::NumericTraits< T >::setValue ( T &  ,
const BaseType ,
const uInt   
)
inlinestatic

Set the nsize()-th numeric value.

Definition at line 246 of file NumericTraits.h.

template<class T>
static uInt casacore::NumericTraits< T >::size ( )
inlinestatic

Number of relevant numeric values.

Definition at line 240 of file NumericTraits.h.

Member Data Documentation

template<class T>
const Double& casacore::NumericTraits< T >::epsilon
static

Relevant minimum and maximum numbers.

Definition at line 235 of file NumericTraits.h.

template<class T>
const Double& casacore::NumericTraits< T >::maximum
static

Definition at line 237 of file NumericTraits.h.

template<class T>
const Double& casacore::NumericTraits< T >::minimum
static

Definition at line 236 of file NumericTraits.h.


The documentation for this class was generated from the following file: