QuantLib: Numeric Type Definitions

The file types.hpp defines a number of numeric types. These are
typedef QL_INTEGER Integer;
typedef QL_BIG_INTEGER BigInteger;
typedef unsigned QL_INTEGER Natural;
typedef unsigned QL_BIG_INTEGER BigNatural;
typedef QL_REAL Real;
typedef Real Decimal;
typedef std::size_t Size;
typedef Real Time;
typedef Real DiscountFactor;
typedef Real Rate;
typedef Real Spread;
typedef Real Volatility;

All these types are based on four basic C++ types

int
long int
double
std::size_t

The reason for QuantLib to define these numeric types is clarity. Take for instance the function

Rate quantoAdjustment(Volatility equityVol, Time t1, Time t2);

From the declaration, it is clear what the different parameters mean, and what the return type is. The following (equivalent) version of that function would not be as clear.

double quantoAdjustment(double equityVol, double t1, double t2);

Another advantage of not using the built-in types is that the precision of the code can be changed. The macros QL_INTEGERQL_BIG_INTEGER and QL_REAL are defined in the header file qldefines.hpp.

#define QL_INTEGER int
#define QL_BIG_INTEGER long
#define QL_REAL double

If you decide that you want to change the default precision, lets say from double to float, you just have to edit qldefines.hpp. But note that you have to do this before compiling and installing the library.

One important warning is in place here. Usually C++ implements strong type enforcement. This means that variables are not implicitly cast from one type to another, unless a type cast operator is explicitly defined. The QuantLib type definitions are not strong in hat respect. For example, a variable of type Rate can be assigned to variable of type Time without any warning by the compiler. This is because Rate and Time are essentially just synonyms for the underlying type double.

With this in mind, the numeric type definitions provided by QuantLib can be useful in writing clear and understandable code. Using the types is not enforced but should become part of good practice.