00001 # ifndef CPPAD_NAN_INCLUDED 00002 # define CPPAD_NAN_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Common Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 /* 00015 $begin nan$$ 00016 $spell 00017 hasnan 00018 cppad 00019 hpp 00020 CppAD 00021 isnan 00022 bool 00023 const 00024 $$ 00025 00026 $section Obtain Nan and Determine if a Value is Nan$$ 00027 00028 $index isnan$$ 00029 $index hasnan$$ 00030 $index nan$$ 00031 00032 $head Syntax$$ 00033 $syntax%# include <cppad/nan.hpp> 00034 %$$ 00035 $syntax%%s% = nan(%z%) 00036 %$$ 00037 $syntax%%b% = isnan(%s%) 00038 %$$ 00039 $syntax%%b% = hasnan(%v%)%$$ 00040 00041 $head Purpose$$ 00042 It obtain and check for the value not a number $code nan$$. 00043 The IEEE standard specifies that a floating point value $italic a$$ 00044 is $code nan$$ if and only if the following returns true 00045 $syntax% 00046 %a% != %a% 00047 %$$ 00048 Some systems do not get this correct, so we also use the fact that 00049 zero divided by zero should result in a $code nan$$. 00050 To be specific, if a value is not equal to itself or 00051 if it is equal to zero divided by zero, it is considered to be a $code nan$$. 00052 00053 $head Include$$ 00054 The file $code cppad/nan.hpp$$ is included by $code cppad/cppad.hpp$$ 00055 but it can also be included separately with out the rest of 00056 the $code CppAD$$ routines. 00057 00058 $head nan$$ 00059 This routine returns a $code nan$$ with the same type as $italic z$$. 00060 00061 $subhead z$$ 00062 The argument $italic z$$ has prototype 00063 $syntax% 00064 const %Scalar% &%z% 00065 %$$ 00066 and its value is zero 00067 (see $cref/Scalar/nan/Scalar/$$ for the definition of $italic Scalar$$). 00068 00069 $subhead s$$ 00070 The return value $italic s$$ has prototype 00071 $syntax% 00072 %Scalar% %s% 00073 %$$ 00074 It is the value $code nan$$ for this floating point type. 00075 00076 $head isnan$$ 00077 This routine determines if a scalar value is $code nan$$. 00078 00079 $subhead s$$ 00080 The argument $italic s$$ has prototype 00081 $syntax% 00082 const %Scalar% %s% 00083 %$$ 00084 00085 $subhead b$$ 00086 The return value $italic b$$ has prototype 00087 $syntax% 00088 bool %b% 00089 %$$ 00090 It is true if the value $italic s$$ is $code nan$$. 00091 00092 $head hasnan$$ 00093 This routine determines if a 00094 $cref/SimpleVector/$$ has an element that is $code nan$$. 00095 00096 $subhead v$$ 00097 The argument $italic v$$ has prototype 00098 $syntax% 00099 const %Vector% &%v% 00100 %$$ 00101 (see $cref/Vector/nan/Vector/$$ for the definition of $italic Vector$$). 00102 00103 $subhead b$$ 00104 The return value $italic b$$ has prototype 00105 $syntax% 00106 bool %b% 00107 %$$ 00108 It is true if the vector $italic v$$ has a $code nan$$. 00109 00110 $head Scalar$$ 00111 The type $italic Scalar$$ must support the following operations; 00112 $table 00113 $bold Operation$$ $cnext $bold Description$$ $rnext 00114 $syntax%%a% / %b%$$ $cnext 00115 division operator (returns a $italic Scalar$$ object) 00116 $rnext 00117 $syntax%%a% == %b%$$ $cnext 00118 equality operator (returns a $code bool$$ object) 00119 $rnext 00120 $syntax%%a% != %b%$$ $cnext 00121 not equality operator (returns a $code bool$$ object) 00122 $tend 00123 Note that the division operator will be used with $italic a$$ and $italic b$$ 00124 equal to zero. For some types (e.g. $code int$$) this may generate 00125 an exception. No attempt is made to catch any such exception. 00126 00127 $head Vector$$ 00128 The type $italic Vector$$ must be a $xref/SimpleVector/$$ class with 00129 elements of type $italic Scalar$$. 00130 00131 $end 00132 */ 00133 00134 namespace CppAD { // BEGIN CppAD namespace 00135 00136 template <class Scalar> 00137 inline Scalar nan(const Scalar &zero) 00138 { return zero / zero; 00139 } 00140 00141 template <class Scalar> 00142 inline bool isnan(const Scalar &s) 00143 { static Scalar scalar_nan = nan( Scalar(0) ); 00144 return (s != s) || (s == scalar_nan); 00145 } 00146 00147 template <class Vector> 00148 bool hasnan(const Vector &v) 00149 { 00150 typedef typename Vector::value_type Scalar; 00151 00152 bool found_nan; 00153 size_t i; 00154 i = v.size(); 00155 found_nan = false; 00156 while(i--) 00157 found_nan |= isnan(v[i]); 00158 return found_nan; 00159 } 00160 00161 } // End CppAD namespace 00162 00163 # endif