CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_NAN_INCLUDED 00003 # define CPPAD_NAN_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 /* 00016 $begin nan$$ 00017 $spell 00018 hasnan 00019 cppad 00020 hpp 00021 CppAD 00022 isnan 00023 bool 00024 const 00025 $$ 00026 00027 $section Obtain Nan or Determine if a Value is Nan$$ 00028 00029 $index isnan$$ 00030 $index hasnan$$ 00031 $index nan$$ 00032 00033 $head Syntax$$ 00034 $codei%# include <cppad/nan.hpp> 00035 %$$ 00036 $icode%s% = nan(%z%) 00037 %$$ 00038 $icode%b% = isnan(%s%) 00039 %$$ 00040 $icode%b% = hasnan(%v%)%$$ 00041 00042 $head Purpose$$ 00043 It obtain and check for the value not a number $code nan$$. 00044 The IEEE standard specifies that a floating point value $icode a$$ 00045 is $code nan$$ if and only if the following returns true 00046 $codei% 00047 %a% != %a% 00048 %$$ 00049 00050 $head Include$$ 00051 The file $code cppad/nan.hpp$$ is included by $code cppad/cppad.hpp$$ 00052 but it can also be included separately with out the rest of 00053 the $code CppAD$$ routines. 00054 00055 $subhead Macros$$ 00056 $index macro, nan$$ 00057 $index macro, isnan$$ 00058 $index nan, macro$$ 00059 $index isnan, macro$$ 00060 Some C++ compilers use preprocessor symbols called $code nan$$ 00061 and $code isnan$$. 00062 These preprocessor symbols will no longer be defined after 00063 this file is included. 00064 00065 $head nan$$ 00066 This routine returns a $code nan$$ with the same type as $icode z$$. 00067 00068 $subhead z$$ 00069 The argument $icode z$$ has prototype 00070 $codei% 00071 const %Scalar% &%z% 00072 %$$ 00073 and its value is zero 00074 (see $cref/Scalar/nan/Scalar/$$ for the definition of $icode Scalar$$). 00075 00076 $subhead s$$ 00077 The return value $icode s$$ has prototype 00078 $codei% 00079 %Scalar% %s% 00080 %$$ 00081 It is the value $code nan$$ for this floating point type. 00082 00083 $head isnan$$ 00084 This routine determines if a scalar value is $code nan$$. 00085 00086 $subhead s$$ 00087 The argument $icode s$$ has prototype 00088 $codei% 00089 const %Scalar% %s% 00090 %$$ 00091 00092 $subhead b$$ 00093 The return value $icode b$$ has prototype 00094 $codei% 00095 bool %b% 00096 %$$ 00097 It is true if the value $icode s$$ is $code nan$$. 00098 00099 $head hasnan$$ 00100 This routine determines if a 00101 $cref SimpleVector$$ has an element that is $code nan$$. 00102 00103 $subhead v$$ 00104 The argument $icode v$$ has prototype 00105 $codei% 00106 const %Vector% &%v% 00107 %$$ 00108 (see $cref/Vector/nan/Vector/$$ for the definition of $icode Vector$$). 00109 00110 $subhead b$$ 00111 The return value $icode b$$ has prototype 00112 $codei% 00113 bool %b% 00114 %$$ 00115 It is true if the vector $icode v$$ has a $code nan$$. 00116 00117 $head Scalar$$ 00118 The type $icode Scalar$$ must support the following operations; 00119 $table 00120 $bold Operation$$ $cnext $bold Description$$ $rnext 00121 $icode%a% / %b%$$ $cnext 00122 division operator (returns a $icode Scalar$$ object) 00123 $rnext 00124 $icode%a% == %b%$$ $cnext 00125 equality operator (returns a $code bool$$ object) 00126 $rnext 00127 $icode%a% != %b%$$ $cnext 00128 not equality operator (returns a $code bool$$ object) 00129 $tend 00130 Note that the division operator will be used with $icode a$$ and $icode b$$ 00131 equal to zero. For some types (e.g. $code int$$) this may generate 00132 an exception. No attempt is made to catch any such exception. 00133 00134 $head Vector$$ 00135 The type $icode Vector$$ must be a $cref SimpleVector$$ class with 00136 elements of type $icode Scalar$$. 00137 00138 $children% 00139 example/nan.cpp 00140 %$$ 00141 $head Example$$ 00142 The file $cref nan.cpp$$ 00143 contains an example and test of this routine. 00144 It returns true if it succeeds and false otherwise. 00145 00146 $end 00147 */ 00148 00149 # include <cstddef> 00150 # include <cppad/local/cppad_assert.hpp> 00151 00152 // needed before one can use CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL 00153 # include <cppad/thread_alloc.hpp> 00154 00155 /* 00156 # define nan There must be a define for every CppAD undef 00157 */ 00158 # ifdef nan 00159 # undef nan 00160 # endif 00161 00162 /* 00163 # define isnan There must be a define for every CppAD undef 00164 */ 00165 # ifdef isnan 00166 # undef isnan 00167 # endif 00168 00169 namespace CppAD { // BEGIN CppAD namespace 00170 00171 template <class Scalar> 00172 inline Scalar nan(const Scalar &zero) 00173 { return zero / zero; 00174 } 00175 00176 template <class Scalar> 00177 inline bool isnan(const Scalar &s) 00178 { return (s != s); 00179 } 00180 00181 template <class Vector> 00182 bool hasnan(const Vector &v) 00183 { 00184 bool found_nan; 00185 size_t i; 00186 i = v.size(); 00187 found_nan = false; 00188 // on MS Visual Studio 2012, CppAD required in front of isnan ? 00189 while(i--) 00190 found_nan |= CppAD::isnan(v[i]); 00191 return found_nan; 00192 } 00193 00194 } // End CppAD namespace 00195 00196 # endif