GNU Scientific Library – Reference Manual: Support for different numeric types

Next: Compatibility with C++, Previous: Alternative optimized functions, Up: Using the library [Index]

### 2.9 Support for different numeric types

Many functions in the library are defined for different numeric types. This feature is implemented by varying the name of the function with a type-related modifier—a primitive form of C++ templates. The modifier is inserted into the function name after the initial module prefix. The following table shows the function names defined for all the numeric types of an imaginary module `gsl_foo` with function `fn`,

    gsl_foo_fn               double
    gsl_foo_long_double_fn   long double
    gsl_foo_float_fn         float
    gsl_foo_long_fn          long
    gsl_foo_ulong_fn         unsigned long
    gsl_foo_int_fn           int
    gsl_foo_uint_fn          unsigned int
    gsl_foo_short_fn         short
    gsl_foo_ushort_fn        unsigned short
    gsl_foo_char_fn          char
    gsl_foo_uchar_fn         unsigned char

The normal numeric precision `double` is considered the default and does not require a suffix. For example, the function `gsl_stats_mean` computes the mean of double precision numbers, while the function `gsl_stats_int_mean` computes the mean of integers.

A corresponding scheme is used for library defined types, such as `gsl_vector` and `gsl_matrix`. In this case the modifier is appended to the type name. For example, if a module defines a new type-dependent struct or typedef `gsl_foo` it is modified for other types in the following way,

    gsl_foo                  double
    gsl_foo_long_double      long double
    gsl_foo_float            float
    gsl_foo_long             long
    gsl_foo_ulong            unsigned long
    gsl_foo_int              int
    gsl_foo_uint             unsigned int
    gsl_foo_short            short
    gsl_foo_ushort           unsigned short
    gsl_foo_char             char
    gsl_foo_uchar            unsigned char

When a module contains type-dependent definitions the library provides individual header files for each type. The filenames are modified as shown in the below. For convenience the default header includes the definitions for all the types. To include only the double precision header file, or any other specific type, use its individual filename.

    #include <gsl/gsl_foo.h>               All types
    #include <gsl/gsl_foo_double.h>        double
    #include <gsl/gsl_foo_long_double.h>   long double
    #include <gsl/gsl_foo_float.h>         float
    #include <gsl/gsl_foo_long.h>          long
    #include <gsl/gsl_foo_ulong.h>         unsigned long
    #include <gsl/gsl_foo_int.h>           int
    #include <gsl/gsl_foo_uint.h>          unsigned int
    #include <gsl/gsl_foo_short.h>         short
    #include <gsl/gsl_foo_ushort.h>        unsigned short
    #include <gsl/gsl_foo_char.h>          char
    #include <gsl/gsl_foo_uchar.h>         unsigned char

Next: Compatibility with C++, Previous: Alternative optimized functions, Up: Using the library [Index]