<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Determine if Two Values Are Nearly Equal</title>
<meta name="description" id="description" content="Determine if Two Values Are Nearly Equal"/>
<meta name="keywords" id="keywords" content=" Nearequal equal near absolute difference relative exercise "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_nearequal_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="cppad_assert.xml" target="_top">Prev</a>
</td><td><a href="near_equal.cpp.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>library</option>
<option>NearEqual</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>library-&gt;</option>
<option>ErrorHandler</option>
<option>NearEqual</option>
<option>speed_test</option>
<option>SpeedTest</option>
<option>time_test</option>
<option>NumericType</option>
<option>CheckNumericType</option>
<option>SimpleVector</option>
<option>CheckSimpleVector</option>
<option>nan</option>
<option>pow_int</option>
<option>Poly</option>
<option>LuDetAndSolve</option>
<option>RombergOne</option>
<option>RombergMul</option>
<option>Runge45</option>
<option>Rosen34</option>
<option>OdeErrControl</option>
<option>OdeGear</option>
<option>OdeGearControl</option>
<option>CppAD_vector</option>
<option>thread_alloc</option>
<option>index_sort</option>
<option>BenderQuad</option>
<option>opt_val_hes</option>
<option>LuRatio</option>
</select>
</td>
<td>
<select onchange='choose_down0(this)'>
<option>NearEqual-&gt;</option>
<option>near_equal.cpp</option>
</select>
</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Syntax</option>
<option>Purpose</option>
<option>x</option>
<option>y</option>
<option>r</option>
<option>a</option>
<option>b</option>
<option>Type</option>
<option>Include Files</option>
<option>Example</option>
<option>Exercise</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Determine if Two Values Are Nearly Equal</big></big></b></center>
<br/>
<b><big><a name="Syntax" id="Syntax">Syntax</a></big></b>


<code><font color="blue"><br/>
# include &lt;cppad/near_equal.hpp&gt;</font></code>
<code><span style='white-space: nowrap'><br/>
</span></code>
<code><i><font color="black"><span style='white-space: nowrap'>b</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;=&#xA0;NearEqual(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>y</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>r</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>a</span></font></i><font color="blue"><span style='white-space: nowrap'>)</span></font></code>



<br/>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
Returns true,
if 
<code><i><font color="black"><span style='white-space: nowrap'>x</span></font></i></code>
 and 
<code><i><font color="black"><span style='white-space: nowrap'>y</span></font></i></code>
 are nearly equal,
and false otherwise.

<br/>
<br/>
<b><big><a name="x" id="x">x</a></big></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>x</span></font></i></code>
 
has one of the following possible prototypes

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&amp;</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;std::complex&lt;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&gt;&#xA0;&amp;</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;<br/>
</span></font></code>
<br/>
<b><big><a name="y" id="y">y</a></big></b>
<br/>
The argument 
<code><i><font color="black"><span style='white-space: nowrap'>y</span></font></i></code>
 
has one of the following possible prototypes

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&amp;</span></font><i><font color="black"><span style='white-space: nowrap'>y</span></font></i><font color="blue"><span style='white-space: nowrap'>,<br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;std::complex&lt;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&gt;&#xA0;&amp;</span></font><i><font color="black"><span style='white-space: nowrap'>y</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;<br/>
</span></font></code>
<br/>
<b><big><a name="r" id="r">r</a></big></b>
<br/>
The relative error criteria 
<code><i><font color="black"><span style='white-space: nowrap'>r</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&amp;</span></font><i><font color="black"><span style='white-space: nowrap'>r</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It must be greater than or equal to zero.
The relative error condition is defined as:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mo stretchy="false">|</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">-</mo>
<mi mathvariant='italic'>y</mi>
<mo stretchy="false">|</mo>
<mo stretchy="false">&#x02264;</mo>
<mi mathvariant='italic'>r</mi>
<mo stretchy="false">(</mo>
<mo stretchy="false">|</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">|</mo>
<mo stretchy="false">+</mo>
<mo stretchy="false">|</mo>
<mi mathvariant='italic'>y</mi>
<mo stretchy="false">|</mo>
<mo stretchy="false">)</mo>
</mrow></math>

<br/>
<b><big><a name="a" id="a">a</a></big></b>
<br/>
The absolute error criteria 
<code><i><font color="black"><span style='white-space: nowrap'>a</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;const&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&amp;</span></font><i><font color="black"><span style='white-space: nowrap'>a</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
It must be greater than or equal to zero.
The absolute error condition is defined as:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mo stretchy="false">|</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">-</mo>
<mi mathvariant='italic'>y</mi>
<mo stretchy="false">|</mo>
<mo stretchy="false">&#x02264;</mo>
<mi mathvariant='italic'>a</mi>
</mrow></math>

<br/>
<b><big><a name="b" id="b">b</a></big></b>
<br/>
The return value 
<code><i><font color="black"><span style='white-space: nowrap'>b</span></font></i></code>
 has prototype

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;bool&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>b</span></font></i><font color="blue"><span style='white-space: nowrap'><br/>
</span></font></code>
If either 
<code><i><font color="black"><span style='white-space: nowrap'>x</span></font></i></code>
 or 
<code><i><font color="black"><span style='white-space: nowrap'>y</span></font></i></code>
 is infinite or not a number, 
the return value is false.
Otherwise, if either the relative or absolute error 
condition (defined above) is satisfied, the return value is true.
Otherwise, the return value is false.

<br/>
<br/>
<b><big><a name="Type" id="Type">Type</a></big></b>
<br/>
The type 
<code><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i></code>
 must be a
<a href="numerictype.xml" target="_top"><span style='white-space: nowrap'>NumericType</span></a>
.
The routine <a href="checknumerictype.xml" target="_top"><span style='white-space: nowrap'>CheckNumericType</span></a>
 will generate
an error message if this is not the case.
In addition, the following operations must be defined objects

<code><i><font color="black"><span style='white-space: nowrap'>a</span></font></i></code>
 and 
<code><i><font color="black"><span style='white-space: nowrap'>b</span></font></i></code>
 of type 
<code><i><font color="black"><span style='white-space: nowrap'>Type</span></font></i></code>
:
<table><tr><td align='left'  valign='top'>

<b>Operation</b>     </td><td align='left'  valign='top'>
 
	<b>Description</b> </td></tr><tr><td align='left'  valign='top'>


<code><i><font color="black"><span style='white-space: nowrap'>a</span></font></i><font color="blue"><span style='white-space: nowrap'>&#xA0;&lt;=&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>b</span></font></i></code>
  </td><td align='left'  valign='top'>
 
	less that or equal operator (returns a <code><font color="blue">bool</font></code> object)
</td></tr>
</table>
<br/>
<b><big><a name="Include Files" id="Include Files">Include Files</a></big></b>
<br/>
The file <code><font color="blue">cppad/near_equal.hpp</font></code> is included by <code><font color="blue">cppad/cppad.hpp</font></code>
but it can also be included separately with out the rest of 
the <code><font color="blue">CppAD</font></code> routines.

<br/>
<br/>
<b><big><a name="Example" id="Example">Example</a></big></b>

<br/>
The file <a href="near_equal.cpp.xml" target="_top"><span style='white-space: nowrap'>near_equal.cpp</span></a>
 contains an example
and test of <code><font color="blue">NearEqual</font></code>.
It return true if it succeeds and false otherwise.

<br/>
<br/>
<b><big><a name="Exercise" id="Exercise">Exercise</a></big></b>

<br/>
Create and run a program that contains the following code:
<code><font color='blue'><pre style='display:inline'> 
	using std::complex;
	using std::cout;
	using std::endl;

	complex&lt;double&gt; one(1., 0), i(0., 1);
	complex&lt;double&gt; x = one / i;
	complex&lt;double&gt; y = - i;
	double          r = 1e-12;
	double          a = 0;
	bool           ok = CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(x, y, r, a);
	if( ok )
		cout &lt;&lt; &quot;Ok&quot;    &lt;&lt; endl;
	else	cout &lt;&lt; &quot;Error&quot; &lt;&lt; endl;
</pre></font></code>



<hr/>Input File: cppad/near_equal.hpp

</body>
</html>
