File: num_limits.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (221 lines) | stat: -rw-r--r-- 7,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
old num_limits.cpp example / test
$spell
$$

$section Numeric Limits: Example and Test$$
$index limits$$
$index example, limits$$
$index test, limits$$

$head Assumption$$
This code assumes that the decimal point is infront of the mantissa.
Hence dividing the minimum normalized value looses precision,
while multiplying the maximum normalized value results in infinity.

$head Externals$$
This example using external routines to get and set values
so that the compiler does not set the correspdong code and optimize
it out.

old verbatim%example/num_limits.cpp%0%// BEGIN C++%// END C++%1%$$

$end
*/
// BEGIN C++
// Complex examples should suppress conversion warnings
# include <cppad/wno_conversion.hpp>

# ifdef _MSC_VER
// Suppress Microsoft compiler warning about possible loss of precision,
// in the constructors (when converting to std::complex<float>)
//    Type one = 1
//    Type two = 2
// 1 and 2 are small enough so no loss of precision when converting to float.
# pragma warning(disable:4244)
# endif

# include <cppad/cppad.hpp>
# include <complex>
# include "extern_value.hpp"

namespace {
   using CppAD::vector;
   using CppAD::abs_geq;

   template <class Type>
   Type add_one(const Type& value)
   {  return( Type(1) + value ); }
   // -----------------------------------------------------------------
   template <class Type>
   bool check_epsilon(void)
   {  bool ok    = true;
      typedef extern_value<Type> value;
      value eps( CppAD::numeric_limits<Type>::epsilon() );
      value one( Type(1) );
      value two( Type(2) );
      value tmp( Type(0) );
      //
      tmp.set( add_one( eps.get() / two.get() ) );
      ok        &= one.get() == tmp.get();
      //
      tmp.set( add_one( eps.get() ) );
      ok        &= one.get() != tmp.get();
      return ok;
   }
   // -----------------------------------------------------------------
   template <class Type>
   bool check_min(void)
   {  bool ok    = true;
      typedef extern_value<Type> value;
      value min( CppAD::numeric_limits<Type>::min() );
      value eps3( Type(3) * CppAD::numeric_limits<Type>::epsilon() );
      value one( Type(1) );
      value hun( Type(100) );
      value tmp( Type(0) );
      //
      tmp.set( min.get() / hun.get() );
      tmp.set( tmp.get() * hun.get() );
      ok        &= abs_geq(tmp.get()/min.get() - one.get(), eps3.get());
      //
      tmp.set( min.get() * hun.get() );
      tmp.set( tmp.get() / hun.get() );
      ok        &= ! abs_geq(tmp.get()/min.get() - one.get(), eps3.get());
      return ok;
   }

   // -----------------------------------------------------------------
   template <class Type>
   bool check_max(void)
   {  bool ok    = true;
      typedef extern_value<Type> value;
      value max2( CppAD::numeric_limits<Type>::max() / Type(2) );
      value eps3( Type(3) * CppAD::numeric_limits<Type>::epsilon() );
      value one( Type(1) );
      value hun( Type(100) );
      value tmp( Type(0) );

      // In complex case, this operation can result in (inf, 0)
      tmp.set( max2.get() * hun.get() );

      // In complex case, this operaiotn can result in (inf,-nan)
      // (where nan corresponds to inf * 0)
      tmp.set( tmp.get() / hun.get() );

      if( ! CppAD::isnan( tmp.get() ) ) ok &= abs_geq(
         tmp.get() / max2.get() - one.get(), eps3.get()
      );
      //
      tmp.set( max2.get() / hun.get() );
      tmp.set( tmp.get() * hun.get() );
      ok        &= ! abs_geq(tmp.get() / max2.get() - one.get(), eps3.get() );
      return ok;
   }
   // -----------------------------------------------------------------
   template <class Type>
   bool check_quiet_NaN(void)
   {  bool ok    = true;
      typedef extern_value<Type> value;
      value nan( CppAD::numeric_limits<Type>::quiet_NaN() );
      value same( nan.get() );
      //
      ok &= nan.get() != same.get();
      ok &= ! (nan.get() == same.get() );
      //
      return ok;
   }
   // -----------------------------------------------------------------
   template <class Type>
   bool check_infinity(void)
   {  bool ok    = true;
      typedef extern_value<Type> value;
      value inf( CppAD::numeric_limits<Type>::infinity() );
      value hun( Type(100) );

      value tmp( Type(0) );

      tmp.set( inf.get() + hun.get() );
      ok &= inf.get() == tmp.get();

      tmp.set( inf.get() - inf.get() );
      ok &= CppAD::isnan( tmp.get() );
      return ok;
   }
}

bool num_limits(void)
{  bool ok = true;
   using CppAD::AD;

   // -------------------------------------------------------------------
   // epsilon for Base types defined by CppAD
   ok &= check_epsilon<float>();
   ok &= check_epsilon<double>();
   ok &= check_epsilon< std::complex<float> >();
   ok &= check_epsilon< std::complex<double> >();

   // epsilon for some AD types.
   ok &= check_epsilon< AD<float> >();
   ok &= check_epsilon< AD<double> >();
   ok &= check_epsilon<  AD<std::complex<float> > >();
   ok &= check_epsilon<  AD<std::complex<double> > >();

   // -------------------------------------------------------------------
   // min for Base types defined by CppAD
   ok &= check_min<float>();
   ok &= check_min<double>();
   ok &= check_min< std::complex<float> >();
   ok &= check_min< std::complex<double> >();

   // min for some AD types.
   ok &= check_min< AD<float> >();
   ok &= check_min< AD<double> >();
   ok &= check_min<  AD<std::complex<float> > >();
   ok &= check_min<  AD<std::complex<double> > >();

   // -------------------------------------------------------------------
   // max for Base types defined by CppAD
   ok &= check_max<float>();
   ok &= check_max<double>();
   ok &= check_max< std::complex<float> >();
   ok &= check_max< std::complex<double> >();

   // max for some AD types.
   ok &= check_max< AD<float> >();
   ok &= check_max< AD<double> >();
   ok &= check_max< AD< std::complex<float> > >();
   ok &= check_max< AD< std::complex<double> > >();
   // -------------------------------------------------------------------
   // quiet_NaN for Base types defined by CppAD
   ok &= check_quiet_NaN<float>();
   ok &= check_quiet_NaN<double>();
   ok &= check_quiet_NaN< std::complex<float> >();
   ok &= check_quiet_NaN< std::complex<double> >();

   // quiet_NaN for some AD types.
   ok &= check_quiet_NaN< AD<float> >();
   ok &= check_quiet_NaN< AD<double> >();
   ok &= check_quiet_NaN< AD< std::complex<float> > >();
   ok &= check_quiet_NaN< AD< std::complex<double> > >();

   // -------------------------------------------------------------------
   // infinity for Base types defined by CppAD
   ok &= check_infinity<float>();
   ok &= check_infinity<double>();
   ok &= check_infinity< std::complex<float> >();
   ok &= check_infinity< std::complex<double> >();

   // infinity for some AD types.
   ok &= check_infinity< AD<float> >();
   ok &= check_infinity< AD<double> >();
   ok &= check_infinity< AD< std::complex<float> > >();
   ok &= check_infinity< AD< std::complex<double> > >();

   return ok;
}
// END C++