Go to the first, previous, next, last section, table of contents.


11. Using the library

For the following discussion, we will assume that you have installed the CLN source in $CLN_DIR and built it in $CLN_TARGETDIR. For example, for me it's CLN_DIR="$HOME/cln" and CLN_TARGETDIR="$HOME/cln/linuxelf". You might define these as environment variables, or directly substitute the appropriate values.

11.1 Compiler options

Until you have installed CLN in a public place, the following options are needed:

When you compile CLN application code, add the flags

   -I$CLN_DIR/include -I$CLN_TARGETDIR/include

to the C++ compiler's command line (make variable CFLAGS or CXXFLAGS). When you link CLN application code to form an executable, add the flags

   $CLN_TARGETDIR/src/libcln.a

to the C/C++ compiler's command line (make variable LIBS).

If you did a make install, the include files are installed in a public directory (normally /usr/local/include), hence you don't need special flags for compiling. The library has been installed to a public directory as well (normally /usr/local/lib), hence when linking a CLN application it is sufficient to give the flag -lcln.

11.2 Include files

Here is a summary of the include files and their contents.

<cl_object.h>
General definitions, reference counting, garbage collection.
<cl_number.h>
The class cl_number.
<cl_complex.h>
Functions for class cl_N, the complex numbers.
<cl_real.h>
Functions for class cl_R, the real numbers.
<cl_float.h>
Functions for class cl_F, the floats.
<cl_sfloat.h>
Functions for class cl_SF, the short-floats.
<cl_ffloat.h>
Functions for class cl_FF, the single-floats.
<cl_dfloat.h>
Functions for class cl_DF, the double-floats.
<cl_lfloat.h>
Functions for class cl_LF, the long-floats.
<cl_rational.h>
Functions for class cl_RA, the rational numbers.
<cl_integer.h>
Functions for class cl_I, the integers.
<cl_io.h>
Input/Output.
<cl_complex_io.h>
Input/Output for class cl_N, the complex numbers.
<cl_real_io.h>
Input/Output for class cl_R, the real numbers.
<cl_float_io.h>
Input/Output for class cl_F, the floats.
<cl_sfloat_io.h>
Input/Output for class cl_SF, the short-floats.
<cl_ffloat_io.h>
Input/Output for class cl_FF, the single-floats.
<cl_dfloat_io.h>
Input/Output for class cl_DF, the double-floats.
<cl_lfloat_io.h>
Input/Output for class cl_LF, the long-floats.
<cl_rational_io.h>
Input/Output for class cl_RA, the rational numbers.
<cl_integer_io.h>
Input/Output for class cl_I, the integers.
<cl_input.h>
Flags for customizing input operations.
<cl_output.h>
Flags for customizing output operations.
<cl_malloc.h>
cl_malloc_hook, cl_free_hook.
<cl_abort.h>
cl_abort.
<cl_condition.h>
Conditions/exceptions.
<cl_string.h>
Strings.
<cl_symbol.h>
Symbols.
<cl_proplist.h>
Property lists.
<cl_ring.h>
General rings.
<cl_null_ring.h>
The null ring.
<cl_complex_ring.h>
The ring of complex numbers.
<cl_real_ring.h>
The ring of real numbers.
<cl_rational_ring.h>
The ring of rational numbers.
<cl_integer_ring.h>
The ring of integers.
<cl_numtheory.h>
Number threory functions.
<cl_modinteger.h>
Modular integers.
<cl_V.h>
Vectors.
<cl_GV.h>
General vectors.
<cl_GV_number.h>
General vectors over cl_number.
<cl_GV_complex.h>
General vectors over cl_N.
<cl_GV_real.h>
General vectors over cl_R.
<cl_GV_rational.h>
General vectors over cl_RA.
<cl_GV_integer.h>
General vectors over cl_I.
<cl_GV_modinteger.h>
General vectors of modular integers.
<cl_SV.h>
Simple vectors.
<cl_SV_number.h>
Simple vectors over cl_number.
<cl_SV_complex.h>
Simple vectors over cl_N.
<cl_SV_real.h>
Simple vectors over cl_R.
<cl_SV_rational.h>
Simple vectors over cl_RA.
<cl_SV_integer.h>
Simple vectors over cl_I.
<cl_SV_ringelt.h>
Simple vectors of general ring elements.
<cl_univpoly.h>
Univariate polynomials.
<cl_univpoly_integer.h>
Univariate polynomials over the integers.
<cl_univpoly_rational.h>
Univariate polynomials over the rational numbers.
<cl_univpoly_real.h>
Univariate polynomials over the real numbers.
<cl_univpoly_complex.h>
Univariate polynomials over the complex numbers.
<cl_univpoly_modint.h>
Univariate polynomials over modular integer rings.
<cl_timing.h>
Timing facilities.
<cln.h>
Includes all of the above.

11.3 An Example

A function which computes the nth Fibonacci number can be written as follows.

#include <cl_integer.h>
#include <cl_real.h>

// Returns F_n, computed as the nearest integer to
// ((1+sqrt(5))/2)^n/sqrt(5). Assume n>=0.
const cl_I fibonacci (int n)
{
        // Need a precision of ((1+sqrt(5))/2)^-n.
        cl_float_format_t prec = cl_float_format((int)(0.208987641*n+5));
        cl_R sqrt5 = sqrt(cl_float(5,prec));
        cl_R phi = (1+sqrt5)/2;
        return round1( expt(phi,n)/sqrt5 );
}

Let's explain what is going on in detail.

The include file <cl_integer.h> is necessary because the type cl_I is used in the function, and the include file <cl_real.h> is needed for the type cl_R and the floating point number functions. The order of the include files does not matter.

Then comes the function declaration. The argument is an int, the result an integer. The return type is defined as `const cl_I', not simply `cl_I', because that allows the compiler to detect typos like `fibonacci(n) = 100'. It would be possible to declare the return type as const cl_R (real number) or even const cl_N (complex number). We use the most specialized possible return type because functions which call `fibonacci' will be able to profit from the compiler's type analysis: Adding two integers is slightly more efficient than adding the same objects declared as complex numbers, because it needs less type dispatch. Also, when linking to CLN as a non-shared library, this minimizes the size of the resulting executable program.

The result will be computed as expt(phi,n)/sqrt(5), rounded to the nearest integer. In order to get a correct result, the absolute error should be less than 1/2, i.e. the relative error should be less than sqrt(5)/(2*expt(phi,n)). To this end, the first line computes a floating point precision for sqrt(5) and phi.

Then sqrt(5) is computed by first converting the integer 5 to a floating point number and than taking the square root. The converse, first taking the square root of 5, and then converting to the desired precision, would not work in CLN: The square root would be computed to a default precision (normally single-float precision), and the following conversion could not help about the lacking accuracy. This is because CLN is not a symbolic computer algebra system and does not represent sqrt(5) in a non-numeric way.

The type cl_R for sqrt5 and, in the following line, phi is the only possible choice. You cannot write cl_F because the C++ compiler can only infer that cl_float(5,prec) is a real number. You cannot write cl_N because a `round1' does not exist for general complex numbers.

When the function returns, all the local variables in the function are automatically reclaimed (garbage collected). Only the result survives and gets passed to the caller.

11.4 Debugging support

When debugging a CLN application with GNU gdb, two facilities are available from the library:


Go to the first, previous, next, last section, table of contents.