CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
numeric_limits.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_NUMERIC_LIMITS_HPP
2 # define CPPAD_CORE_NUMERIC_LIMITS_HPP
3 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
5 
6 CppAD is distributed under multiple licenses. This distribution is under
7 the terms of the
8  Eclipse Public License Version 1.0.
9 
10 A copy of this license is included in the COPYING file of this distribution.
11 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12 -------------------------------------------------------------------------- */
13 
14 /*
15 ------------------------------------------------------------------------------
16 $begin numeric_limits$$
17 $spell
18  std
19  eps
20  CppAD
21  namespace
22  const
23 $$
24 
25 $section Numeric Limits For an AD and Base Types$$
26 
27 $head Syntax$$
28 $icode%eps% = numeric_limits<%Float%>::epsilon()
29 %$$
30 $icode%min% = numeric_limits<%Float%>::min()
31 %$$
32 $icode%max% = numeric_limits<%Float%>::max()
33 %$$
34 $icode%nan% = numeric_limits<%Float%>::quiet_NaN()
35 %$$
36 $codei%numeric_limits<%Float%>::digits10%$$
37 
38 $head CppAD::numeric_limits$$
39 These functions and have the prototype
40 $codei%
41  static %Float% CppAD::numeric_limits<%Float%>::%fun%(%void%)
42 %$$
43 where $icode fun$$ is
44 $code epsilon$$, $code min$$, $code max$$, and $code quiet_NaN$$.
45 (Note that $code digits10$$ is member variable and not a function.)
46 
47 $head std::numeric_limits$$
48 CppAD does not use a specialization of $code std::numeric_limits$$
49 because this would be to restrictive.
50 The C++ standard specifies that Non-fundamental standard
51 types, such as
52 $cref/std::complex<double>/base_complex.hpp/$$ shall not have specializations
53 of $code std::numeric_limits$$; see Section 18.2 of
54 ISO/IEC 14882:1998(E).
55 In addition, since C++11, a only literal types can have a specialization
56 of $code std::numeric_limits$$.
57 
58 $head Float$$
59 These functions are defined for all $codei%AD<%Base%>%$$,
60 and for all corresponding $icode Base$$ types;
61 see $icode Base$$ type $cref base_limits$$.
62 
63 $head epsilon$$
64 The result $icode eps$$ is equal to machine epsilon and has prototype
65 $codei%
66  %Float% %eps%
67 %$$
68 The file $cref num_limits.cpp$$
69 tests the value $icode eps$$ by checking that the following are true
70 $codei%
71  1 != 1 + %eps%
72  1 == 1 + %eps% / 2
73 %$$
74 where all the values, and calculations, are done with the precision
75 corresponding to $icode Float$$.
76 
77 $head min$$
78 The result $icode min$$ is equal to
79 the minimum positive normalized value and has prototype
80 $codei%
81  %Float% %min%
82 %$$
83 The file $cref num_limits.cpp$$
84 tests the value $icode min$$ by checking that the following are true
85 $codei%
86  abs( ((%min% / 100) * 100) / %min% - 1 ) > 3 * %eps%
87  abs( ((%min% * 100) / 100) / %min% - 1 ) < 3 * %eps%
88 %$$
89 where all the values, and calculations, are done with the precision
90 corresponding to $icode Float$$.
91 
92 $head max$$
93 The result $icode max$$ is equal to
94 the maximum finite value and has prototype
95 $codei%
96  %Float% %max%
97 %$$
98 The file $cref num_limits.cpp$$
99 tests the value $icode max$$ by checking that the following are true
100 $codei%
101  abs( ((%max% * 100) / 100) / %max% - 1 ) > 3 * %eps%
102  abs( ((%max% / 100) * 100) / %max% - 1 ) < 3 * %eps%
103 %$$
104 where all the values, and calculations, are done with the precision
105 corresponding to $icode Float$$.
106 
107 $head quiet_NaN$$
108 The result $icode nan$$ is not a number and has prototype
109 $codei%
110  %Float% %nan%
111 %$$
112 The file $cref num_limits.cpp$$
113 tests the value $icode nan$$ by checking that the following is true
114 $codei%
115  %nan% != %nan%
116 %$$
117 
118 $head digits10$$
119 The member variable $code digits10$$ has prototype
120 $codei%
121  static const int numeric_limits<%Float%>::digits10
122 %$$
123 It is the number of decimal digits that can be represented by a
124 $icode Float$$ value. A number with this many decimal digits can be
125 converted to $icode Float$$ and back to a string,
126 without change due to rounding or overflow.
127 
128 
129 $head Example$$
130 $children%
131  example/general/num_limits.cpp
132 %$$
133 The file
134 $cref num_limits.cpp$$
135 contains an example and test of these functions.
136 
137 $end
138 ------------------------------------------------------------------------------
139 */
140 # include <iostream>
141 
142 # include <cppad/configure.hpp>
143 # include <cppad/core/define.hpp>
144 # include <cppad/core/cppad_assert.hpp>
145 # include <cppad/local/declare_ad.hpp>
146 
147 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
148 /*!
149 \file numeric_limits.hpp
150 File that defines CppAD numeric_limits for AD types
151 */
152 
153 /// All tthese defaults correspond to errors
154 template <class Float>
156 public:
157  /// machine epsilon
158  static Float epsilon(void)
160  false,
161  "numeric_limits<Float>::epsilon() is not specialized for this Float"
162  );
163  return Float(0);
164  }
165  /// minimum positive normalized value
166  static Float min(void)
168  false,
169  "numeric_limits<Float>::min() is not specialized for this Float"
170  );
171  return Float(0);
172  }
173  /// maximum finite value
174  static Float max(void)
176  false,
177  "numeric_limits<Float>::max() is not specialized for this Float"
178  );
179  return Float(0);
180  }
181  /// not a number
182  static Float quiet_NaN(void)
184  false,
185  "numeric_limits<Float>::quiet_NaN() is not specialized for this Float"
186  );
187  return Float(0);
188  }
189  /// number of decimal digits
190  static const int digits10 = -1;
191 };
192 
193 /// Partial specialization that defines limits for for all AD types
194 template <class Base>
195 class numeric_limits< AD<Base> > {
196 public:
197  /// machine epsilon
198  static AD<Base> epsilon(void)
199  { return AD<Base>( numeric_limits<Base>::epsilon() ); }
200  /// minimum positive normalized value
201  static AD<Base> min(void)
202  { return AD<Base>( numeric_limits<Base>::min() ); }
203  /// maximum finite value
204  static AD<Base> max(void)
205  { return AD<Base>( numeric_limits<Base>::max() ); }
206  /// not a number
207  static AD<Base> quiet_NaN(void)
209  /// number of decimal digits
211 };
212 
213 } // END_CPPAD_NAMESPACE
214 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
Define processor symbols and macros that are used by CppAD.
static AD< Base > epsilon(void)
machine epsilon
static Float max(void)
maximum finite value
static const int digits10
number of decimal digits
static Float quiet_NaN(void)
not a number
Definition: ad.hpp:34
static Float epsilon(void)
machine epsilon
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
static AD< Base > max(void)
maximum finite value
static Float min(void)
minimum positive normalized value
CppAD forward declarations; i.e., before definition.
All tthese defaults correspond to errors.
static AD< Base > quiet_NaN(void)
not a number
static AD< Base > min(void)
minimum positive normalized value