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