Using C with CTT's CNL Numerical Library

The C Numerical Library (CNL) is essentially the IMSL Fortran Numerical Library (FNL) converted from Fortran 77 subprograms to C procedures.

CTT conventions for using CNL

Each C program that uses a CNL procedure should begin with

#include <math.h>
#include <imsl.h>

These will provide access to the basic C math functions and the IMSL header file and its CNL prototype statements.

All CNL procedure names begin with imsl_ followed by a type letter:

  1. f_float
  2. d_double
  3. c_complex
  4. z_double complex.

This document illustrates use of the CNL imsl_d_beta procedure to evaluate the beta function of two double variables. The "correct" answer to 30 digits was obtained from Mathematica and is printed exactly for comparison with the CNL value. In this example, the imsl_d_beta function calculates the correct answer to 16 digits, which is what one expects from a double precision function. (The sample C program prints 18 digits as requested, without complaint from the compiler or CNL. However, all digits beyond the 16th should be considered random noise.)

The output from a sample terminal session is shown below, followed by a discussion of the steps. The cttshell alias and environment variables used below are available if you have made the suggested UNIX configuration changes described in Getting Started with CTT: Setting Up Your UNIX Account.

setenv commands in setup files
CTT_DIR /opt/vni/CTT5.0
<100>% cttshell 
<1>% touch betaTest.c 
<2>% make betaTest
cc -mp -fsingle -DANSI
-I/opt/vni/CTT3.0/include -o betaTest betaTest.c
-R/opt/vni/CTT5.0/lib/lib.solaris -L/opt/vni/CTT5.0/lib/lib.solaris
-limslcmathsup_perf -limslcmath -limslcstat -xlic_lib=sunperf -lm
-lsocket -lnsl -lmtsk 
<3>% betaTest
The beta function using the imsl_d_beta procedure is 
  beta = 0.147252692087097320
The beta function (to 30 digits) using Mathematica is 
  beta = 0.147252692087097336727624375866
<4>% exit 
  • The cttshell command starts a new shell with the correct environment for all CTT products.
  • The sample C program that invokes the beta function is stored in a file named betaTest.c (whose listing is given further below).
  • The make betaTest command finds the C source file and creates the executable betaTest. Since the source file has a ".c" extension, make uses a suffix rule to invoke a C compiler. (The specific compiler used is the one defined by the environment variable CC, which has been set to the Sun Studio 11 cc compiler.)
  • The C source program is touched so that its modification time is newer than the executable. That forces the make command to recompile betaTest.c.
  • The executable is run and it reports the correct result.
  • Finally, exiting the shell returns us to the parent shell (command number <101>) with its previous environment variable and alias definitions.

Listing of the betaTest.c sample program


void main() { 
        double a,b;
        printf("The beta function using the imsl_d_beta procedure is\n");
        printf(" beta = %20.18f\n",imsl_d_beta(a,b)); 
        printf("The beta function (to 30 digits) using Mathematica is\n"); 
        printf(" beta = 0.147252692087097336727624375866\n"); }

Search IT Help

My UD Search for forms & applications.