source: trunk/cppad/nan.hpp @ 2870

Last change on this file since 2870 was 2870, checked in by bradbell, 7 years ago

Fix g++ 4.8.1 warnings about unused local types.

check_doxygen.sh: new version of doxygen has warnings that need to be fixed.
check_simple_vector.hpp: fix minor typos in documentation.

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/* $Id: nan.hpp 2870 2013-07-28 17:00:59Z bradbell $ */
2# ifndef CPPAD_NAN_INCLUDED
3# define CPPAD_NAN_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please 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$$
43It obtain and check for the value not a number $code nan$$.
44The IEEE standard specifies that a floating point value $icode a$$
45is $code nan$$ if and only if the following returns true
46$codei%
47        %a% != %a%
48%$$
49
50$head Include$$
51The file $code cppad/nan.hpp$$ is included by $code cppad/cppad.hpp$$
52but it can also be included separately with out the rest of
53the $code CppAD$$ routines.
54
55$subhead Macros$$
56$index macro, nan$$
57$index macro,  isnan$$
58$index nan, macro$$
59$index isnan, macro$$
60Some C++ compilers use preprocessor symbols called $code nan$$
61and $code isnan$$.
62These preprocessor symbols will no longer be defined after
63this file is included.
64
65$head nan$$
66This routine returns a $code nan$$ with the same type as $icode z$$.
67
68$subhead z$$
69The argument $icode z$$ has prototype
70$codei%
71        const %Scalar% &%z%
72%$$
73and its value is zero
74(see $cref/Scalar/nan/Scalar/$$ for the definition of $icode Scalar$$).
75
76$subhead s$$
77The return value $icode s$$ has prototype
78$codei%
79        %Scalar% %s%
80%$$
81It is the value $code nan$$ for this floating point type.
82
83$head isnan$$
84This routine determines if a scalar value is $code nan$$.
85
86$subhead s$$
87The argument $icode s$$ has prototype
88$codei%
89        const %Scalar% %s%
90%$$
91
92$subhead b$$
93The return value $icode b$$ has prototype
94$codei%
95        bool %b%
96%$$
97It is true if the value $icode s$$ is $code nan$$.
98
99$head hasnan$$
100This routine determines if a
101$cref SimpleVector$$ has an element that is $code nan$$.
102
103$subhead v$$
104The 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$$
111The return value $icode b$$ has prototype
112$codei%
113        bool %b%
114%$$
115It is true if the vector $icode v$$ has a $code nan$$.
116
117$head Scalar$$
118The 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
130Note that the division operator will be used with $icode a$$ and $icode b$$
131equal to zero. For some types (e.g. $code int$$) this may generate
132an exception. No attempt is made to catch any such exception.
133
134$head Vector$$
135The type $icode Vector$$ must be a $cref SimpleVector$$ class with
136elements of type $icode Scalar$$.
137
138$children%
139        example/nan.cpp
140%$$
141$head Example$$
142The file $cref nan.cpp$$
143contains an example and test of this routine.
144It 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
162namespace CppAD { // BEGIN CppAD namespace
163
164template <class Scalar>
165inline Scalar nan(const Scalar &zero)
166{       return zero / zero;
167}
168
169template <class Scalar>
170inline bool isnan(const Scalar &s)
171{       return (s != s);
172}
173
174template <class Vector>
175bool 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
Note: See TracBrowser for help on using the repository browser.