Approximate Comparison of Floating Point Numbers - GNU Scientific Library -- Reference Manual
Description: GNU Scientific Library -- Reference Manual

Previous: Maximum and Minimum functions, Up: Mathematical Functions

### 4.8 Approximate Comparison of Floating Point Numbers

It is sometimes useful to be able to compare two floating point numbers approximately, to allow for rounding and truncation errors. The following function implements the approximate floating-point comparison algorithm proposed by D.E. Knuth in Section 4.2.2 of Seminumerical Algorithms (3rd edition).

— Function: int gsl_fcmp (double x, double y, double epsilon)

> This function determines whether x and y are approximately equal to a relative accuracy epsilon.

> The relative accuracy is measured using an interval of size 2 \delta, where \delta = 2^k \epsilon and k is the maximum base-2 exponent of x and y as computed by the function `frexp`.

> If x and y lie within this interval, they are considered approximately equal and the function returns 0. Otherwise if x < y, the function returns -1, or if x > y, the function returns +1.

> Note that x and y are compared to relative accuracy, so this function is not suitable for testing whether a value is approximately zero.

> The implementation is based on the package `fcmp` by T.C. Belding.

The GNU Scientific Library - a free numerical library licensed under the GNU GPL
Back to the GNU Scientific Library Homepage