CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
nan.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_NAN_HPP
2 # define CPPAD_UTILITY_NAN_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin nan$$
16 $spell
17  hasnan
18  cppad
19  hpp
20  CppAD
21  isnan
22  bool
23  const
24 $$
25 
26 $section Obtain Nan or Determine if a Value is Nan$$
27 
28 $head Syntax$$
29 $codei%# include <cppad/utility/nan.hpp>
30 %$$
31 $icode%b% = isnan(%s%)
32 %$$
33 $icode%b% = hasnan(%v%)%$$
34 
35 $head Purpose$$
36 It obtain and check for the value not a number $code nan$$.
37 The IEEE standard specifies that a floating point value $icode a$$
38 is $code nan$$ if and only if the following returns true
39 $codei%
40  %a% != %a%
41 %$$
42 
43 $head Include$$
44 The file $code cppad/nan.hpp$$ is included by $code cppad/cppad.hpp$$
45 but it can also be included separately with out the rest of
46 the $code CppAD$$ routines.
47 
48 $subhead Macros$$
49 Some C++ compilers use preprocessor symbols called $code nan$$
50 and $code isnan$$.
51 These preprocessor symbols will no longer be defined after
52 this file is included.
53 
54 $head isnan$$
55 This routine determines if a scalar value is $code nan$$.
56 
57 $subhead s$$
58 The argument $icode s$$ has prototype
59 $codei%
60  const %Scalar% %s%
61 %$$
62 
63 $subhead b$$
64 The return value $icode b$$ has prototype
65 $codei%
66  bool %b%
67 %$$
68 It is true if the value $icode s$$ is $code nan$$.
69 
70 $head hasnan$$
71 This routine determines if a
72 $cref SimpleVector$$ has an element that is $code nan$$.
73 
74 $subhead v$$
75 The argument $icode v$$ has prototype
76 $codei%
77  const %Vector% &%v%
78 %$$
79 (see $cref/Vector/nan/Vector/$$ for the definition of $icode Vector$$).
80 
81 $subhead b$$
82 The return value $icode b$$ has prototype
83 $codei%
84  bool %b%
85 %$$
86 It is true if the vector $icode v$$ has a $code nan$$.
87 
88 
89 $head nan(zero)$$
90 
91 $subhead Deprecated 2015-10-04$$
92 This routine has been deprecated, use CppAD numeric limits
93 $cref/quiet_NaN/numeric_limits/quiet_NaN/$$ in its place.
94 
95 $subhead Syntax$$
96 $icode%s% = nan(%z%)
97 %$$
98 
99 $subhead z$$
100 The argument $icode z$$ has prototype
101 $codei%
102  const %Scalar% &%z%
103 %$$
104 and its value is zero
105 (see $cref/Scalar/nan/Scalar/$$ for the definition of $icode Scalar$$).
106 
107 $subhead s$$
108 The return value $icode s$$ has prototype
109 $codei%
110  %Scalar% %s%
111 %$$
112 It is the value $code nan$$ for this floating point type.
113 
114 $head Scalar$$
115 The type $icode Scalar$$ must support the following operations;
116 $table
117 $bold Operation$$ $cnext $bold Description$$ $rnext
118 $icode%a% / %b%$$ $cnext
119  division operator (returns a $icode Scalar$$ object)
120 $rnext
121 $icode%a% == %b%$$ $cnext
122  equality operator (returns a $code bool$$ object)
123 $rnext
124 $icode%a% != %b%$$ $cnext
125  not equality operator (returns a $code bool$$ object)
126 $tend
127 Note that the division operator will be used with $icode a$$ and $icode b$$
128 equal to zero. For some types (e.g. $code int$$) this may generate
129 an exception. No attempt is made to catch any such exception.
130 
131 $head Vector$$
132 The type $icode Vector$$ must be a $cref SimpleVector$$ class with
133 elements of type $icode Scalar$$.
134 
135 $children%
136  example/utility/nan.cpp
137 %$$
138 $head Example$$
139 The file $cref nan.cpp$$
140 contains an example and test of this routine.
141 It returns true if it succeeds and false otherwise.
142 
143 $end
144 */
145 
146 # include <cstddef>
147 # include <cppad/core/cppad_assert.hpp>
148 
149 // needed before one can use CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL
151 
152 /*
153 # define nan There must be a define for every CppAD undef
154 */
155 # ifdef nan
156 # undef nan
157 # endif
158 
159 /*
160 # define isnan There must be a define for every CppAD undef
161 */
162 # ifdef isnan
163 # undef isnan
164 # endif
165 
166 namespace CppAD { // BEGIN CppAD namespace
167 
168 template <class Scalar>
169 inline bool isnan(const Scalar &s)
170 { return (s != s);
171 }
172 
173 template <class Vector>
174 bool hasnan(const Vector &v)
175 {
176  bool found_nan;
177  size_t i;
178  i = v.size();
179  found_nan = false;
180  // on MS Visual Studio 2012, CppAD required in front of isnan ?
181  while(i--)
182  found_nan |= CppAD::isnan(v[i]);
183  return found_nan;
184 }
185 
186 template <class Scalar>
187 inline Scalar nan(const Scalar &zero)
188 { return zero / zero;
189 }
190 
191 } // End CppAD namespace
192 
193 # endif
Scalar nan(const Scalar &zero)
Definition: nan.hpp:187
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
bool isnan(const Scalar &s)
Definition: nan.hpp:169
bool hasnan(const Vector &v)
Definition: nan.hpp:174
File used to define the CppAD multi-threading allocator class.