3
<title>Approximate Comparison of Floating Point Numbers - GNU Scientific Library -- Reference Manual</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="GNU Scientific Library -- Reference Manual">
6
<meta name="generator" content="makeinfo 4.8">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="Mathematical-Functions.html#Mathematical-Functions" title="Mathematical Functions">
9
<link rel="prev" href="Maximum-and-Minimum-functions.html#Maximum-and-Minimum-functions" title="Maximum and Minimum functions">
10
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The GSL Team.
14
Permission is granted to copy, distribute and/or modify this document
15
under the terms of the GNU Free Documentation License, Version 1.2 or
16
any later version published by the Free Software Foundation; with the
17
Invariant Sections being ``GNU General Public License'' and ``Free Software
18
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
19
and with the Back-Cover Text being (a) (see below). A copy of the
20
license is included in the section entitled ``GNU Free Documentation
23
(a) The Back-Cover Text is: ``You have freedom to copy and modify this
24
GNU Manual, like GNU software.''-->
25
<meta http-equiv="Content-Style-Type" content="text/css">
26
<style type="text/css"><!--
27
pre.display { font-family:inherit }
28
pre.format { font-family:inherit }
29
pre.smalldisplay { font-family:inherit; font-size:smaller }
30
pre.smallformat { font-family:inherit; font-size:smaller }
31
pre.smallexample { font-size:smaller }
32
pre.smalllisp { font-size:smaller }
33
span.sc { font-variant:small-caps }
34
span.roman { font-family:serif; font-weight:normal; }
35
span.sansserif { font-family:sans-serif; font-weight:normal; }
41
<a name="Approximate-Comparison-of-Floating-Point-Numbers"></a>
42
Previous: <a rel="previous" accesskey="p" href="Maximum-and-Minimum-functions.html#Maximum-and-Minimum-functions">Maximum and Minimum functions</a>,
43
Up: <a rel="up" accesskey="u" href="Mathematical-Functions.html#Mathematical-Functions">Mathematical Functions</a>
47
<h3 class="section">4.8 Approximate Comparison of Floating Point Numbers</h3>
49
<p>It is sometimes useful to be able to compare two floating point numbers
50
approximately, to allow for rounding and truncation errors. The following
51
function implements the approximate floating-point comparison algorithm
52
proposed by D.E. Knuth in Section 4.2.2 of <cite>Seminumerical
53
Algorithms</cite> (3rd edition).
56
— Function: int <b>gsl_fcmp</b> (<var>double x, double y, double epsilon</var>)<var><a name="index-gsl_005ffcmp-128"></a></var><br>
57
<blockquote><p><a name="index-approximate-comparison-of-floating-point-numbers-129"></a><a name="index-safe-comparison-of-floating-point-numbers-130"></a><a name="index-floating-point-numbers_002c-approximate-comparison-131"></a>This function determines whether x and y are approximately
58
equal to a relative accuracy <var>epsilon</var>.
60
<p>The relative accuracy is measured using an interval of size 2
61
\delta, where \delta = 2^k \epsilon and k is the
62
maximum base-2 exponent of x and y as computed by the
63
function <code>frexp()</code>.
65
<p>If x and y lie within this interval, they are considered
66
approximately equal and the function returns 0. Otherwise if x <
67
y, the function returns -1, or if x > y, the function returns
70
<p>The implementation is based on the package <code>fcmp</code> by T.C. Belding.
71
</p></blockquote></div>