Helper class containing typedefs associated with numeric types, useful when creating a new numeric type. More...
#include <rw/math/numtrait.h>
Public Types | |
typedef T | c_type |
typedef T | calc_type |
typedef T(* | mathFunType) (T) |
typedef norm_type(* | mathFunType2) (T) |
typedef T | norm_type |
typedef T | numeric_type |
typedef T | promote_type |
The rw_numeric_traits class is a helper class that contains only typedef information. The generic rw_numeric_traits assumes that all the typedefs are set to T
. Since this is not desirable for many types, we specialize for each numeric type that is used in the library.
The specializations used in the library are contained in the file rw/math/numtrait.h
. For an overview of how to add a new numeric type, see Section 2.2.2, "Adding New Numeric Types" in the Essential Math Module User's Guide.
There are seven public typedefs in each specialized rw_numeric_traits class. Each typedef must be defined in a given specialization for proper results. The generic class is:
typedef T rw_numeric_traits< T >::c_type |
This is the companion to numeric_type. If T is a char-like
type, it should be set to char
; otherwise, it should be set to T.
typedef T rw_numeric_traits< T >::calc_type |
For some aggregate functions such as sum
, a higher-precision numeric type can be used for intermediate calculations. This is intended mostly for floating point types to reduce round-off error in computations. For example, rw_numeric_traits<float>::calc_type is double
because this makes the sum of a vector of floats
less susceptible to round-off error.
typedef T(* rw_numeric_traits< T >::mathFunType) (T) |
The member functions RWMathVec<T>::apply(), RWGenMat<T>::apply(), and RWMathArray<T>::apply() take a function pointer of type mathFunType These functions are used to apply a global function to each element of a collection. The compiler must make an exact match for the apply() function to work, and since most global numerical functions take a double
and return a double
, the mathFunType is set to double
in almost all cases except complex. If type T is readily converted to type double
and you want to use the apply() function with built-in functions, mathFunType should be set by:
typedef double (*mathFunType)(double)
;
If, on the other hand, your type T uses specific functions that take T and return T, the default setting is appropriate. Note that some compilers distinguish and do not match between the above typedef and:
typedef double (*mathFunType)(const double&)
;
Be sure that your definition of mathFunType is consistent with the functions you use in apply().
typedef norm_type(* rw_numeric_traits< T >::mathFunType2) (T) |
The member functions RWMathVec<T>::apply2(), RWGenMat<T>::apply2(), and RWMathArray<T>::apply2() take a function pointer of type mathFunType2. The discussion of mathFunType applies equally here, with the exception of the return type. The return type for the apply2() function is type norm_type. The mathFunType and mathFunType2 definitions are most often the same except that the return type of mathFunType2 is norm_type.
typedef T rw_numeric_traits< T >::norm_type |
The return type of abs() or similar numerical norm-like
functions, such as rw_numeric_traits<DComplex>::norm_type, is double
because abs(DComplex) returns a double
. This is most often double
for built-in types.
typedef T rw_numeric_traits< T >::numeric_type |
For most types this should be set to T. This typedef is aimed to improve I/O operations for char based collections. If T
is a char-like
type, but it is used in any RWMathVec, RWGenMat, or RWMathArray, the typedef should be set to int
.
typedef T rw_numeric_traits< T >::promote_type |
Automatic conversions and templates do not work well together because they readily lead to ambiguity errors. For that reason, automatic conversions of the collection classes are limited to one promotion type per numeric type T. The promote_type
setting allows an RWMathVec to be converted automatically to RWMathVec<rw_numeric_traits<T>::promote_type>. In other words, rw_numeric_traits<float>::promote_type is double
, while rw_numeric_traits<double>::promote_type is DComplex. This typedef is designed to assist in backward compatibility, and may become obsolete when new language features become available.
Copyright © 2021 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved. |