Defining a New Numeric Type
If you create an entirely new numeric type not available from your compiler, your numeric class must meet minimum requirements to have full functionality in the Essential Math Module. We include an example numeric class, Rational, located in file rational.h in your Rogue Wave examples directory. You can examine this file and use it as a framework for building your own numeric type.
To meet the minimum requirements for full functionality in the Essential Math Module, a numeric type Num must:
Be constructible either from 0 or 1, or with no arguments; for example,
Num x,
Num y(0), and
Num z(1) are all legal statements.
Define all arithmetic operators; that is,
x+y,
x-y,
x*y,
x/y, and
-x.
Define all arithmetic with assignment operators; for example,
x+=y,
x-=y,
x*=y,
x/=y.
Define all equality and nonequality boolean operators; for example,
x==y,
x!=y.
In addition, there may be other requirements for a numeric type, depending on your application. These requirements apply only when a type is used in a particular part of the library. For example, the numeric type Num may also be required to:
Define global functions. Any templatized global function used with type
Num must be defined as a function that takes and returns a type
Num. In our example,
Rational, we define
sin(Rational), which returns a
Rational that is of course an approximation. Defining this function enables the use of the library function
sin(const RWMathVec<Rational>&). The
Rational class also has an example of a nontemplatized function definition
linfNorm(const RWMathVec<Rational>&) that must be defined explicitly in order to use it.
Define
file/stream shift operators. The choices for I/O include
istream,
ostream,
vistream,
vostream, and
RWFile. To use any of these on a collection of type
Num, the corresponding shift operator must be defined for type
Num. Examples of all six operators are given in the
Rational class.
Define an
rw_numeric_traits class. You may also need to add an
rw_numeric_traits class specialization for your numeric type, so that calculations for this class are handled properly. A default class is provided, but a specialized class may produce better results. You can include the
rw_numeric_traits specialization at the end of the numeric type header file, as in the
rational.h file. This is the preferred place to define this specialization, but you can assign a different place as explained in the next section.