The library follows the thread-safe error reporting conventions of the
POSIX Threads library. Functions return a non-zero error code to
indicate an error and 0
to indicate success.
int status = gsl_function(...) if (status) { /* an error occurred */ ..... /* status value specifies the type of error */ }
The routines report an error whenever they cannot perform the task requested of them. For example, a root-finding function would return a non-zero error code if could not converge to the requested accuracy, or exceeded a limit on the number of iterations. Situations like this are a normal occurrence when using any mathematical library and you should check the return status of the functions that you call.
Whenever a routine reports an error the return value specifies the
type of error. The return value is analogous to the value of the
variable errno
in the C library. However, the C library's
errno
is a global variable, which is not thread-safe (There can
be only one instance of a global variable per program. Different
threads of execution may overwrite errno
simultaneously).
Returning the error number directly avoids this problem. The caller can
examine the return code and decide what action to take, including
ignoring the error if it is not considered serious.
The error code numbers are defined in the file `gsl_errno.h'. They
all have the prefix GSL_
and expand to non-zero constant integer
values. Many of the error codes use the same base name as a
corresponding error code in C library. Here are some of the most common
error codes,
malloc
.
int status = gsl_fft_complex_radix2_forward (data, n); if (status) { if (status == GSL_EINVAL) { fprintf (stderr, "invalid argument, n=%d\n", n); } else { fprintf (stderr, "failed, gsl_errno=%d\n", status); } exit (-1); }
The function gsl_fft_complex_radix2
only accepts integer lengths
which are a power of two. If the variable n
is not a power
of two then the call to the library function will return
GSL_EINVAL
, indicating that the length argument is invalid. The
else
clause catches any other possible errors.
The error codes can be converted into an error message using the
function gsl_strerror
.
printf("error: %s\n", gsl_strerror (status));
would print an error message like error: output range error
for a
status value of GSL_ERANGE
.