File: base_alloc.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 (85 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (2)
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
// 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
// ----------------------------------------------------------------------------
# include "../../example/general/base_alloc.hpp"
# include <cppad/cppad.hpp>

namespace { // BEGIN empty namespace

bool test_parameter(void)
{  bool ok = true;
   typedef CppAD::AD<base_alloc> my_ad;

   // y = x + 0 + 1 + 2 + ... + N-1
   CppAD::vector<my_ad>   a_x, a_y;
   a_x.resize(1);
   a_x[0] = my_ad(1.);
   CppAD::Independent(a_x);
   a_y.resize(1);
   a_y[0] = a_x[0];

   // create a new parameter for each iteration of this loop
   size_t i, N = 50;
   for(i = 0; i < N; i++)
      a_y[0] += double(i);

   CppAD::ADFun<base_alloc> f(a_x, a_y);

   return ok;
}

bool test_numeric_limits(void)
{  bool ok = true;
   typedef CppAD::AD<base_alloc> my_ad;
   //
   base_alloc eps = Value( CppAD::numeric_limits<my_ad>::epsilon() );
   ok            &= *eps.ptrdbl_ == std::numeric_limits<double>::epsilon();
   //
   base_alloc min = Value( CppAD::numeric_limits<my_ad>::min() );
   ok            &= *min.ptrdbl_ == std::numeric_limits<double>::min();
   //
   base_alloc max = Value( CppAD::numeric_limits<my_ad>::max() );
   ok            &= *max.ptrdbl_ == std::numeric_limits<double>::max();
   //
   base_alloc nan = Value( CppAD::numeric_limits<my_ad>::quiet_NaN() );
   ok            &= *nan.ptrdbl_ != *nan.ptrdbl_;
   //
   int   digits10 = CppAD::numeric_limits<my_ad>::digits10;
   ok            &= digits10 == std::numeric_limits<double>::digits10;
   //
   return ok;
}

bool test_to_string(void)
{  bool ok = true;
   typedef CppAD::AD<base_alloc> my_ad;
   //
   double      dbl_pi = 4.0 * std::atan(1.0);
   my_ad       ad_pi  = my_ad(dbl_pi);
   std::string str_pi = to_string( ad_pi );
   //
   // Check the length of the string "3.1415...". One extra character
   // for machine epsilon precision and another for the decimal point.
   ok &= str_pi.size() == size_t( 2 + std::numeric_limits<double>::digits10 );
   //
   // check value
   double eps   = std::numeric_limits<double>::epsilon();
   double check = std::atof( str_pi.c_str() );
   ok          &= (check / dbl_pi - 1.0) <= eps;
   //
   return ok;
}


} // END empty namespace

bool base_alloc_test(void)
{  bool ok = true;
   //
   ok     &= test_parameter();
   ok     &= test_numeric_limits();
   ok     &= test_to_string();
   //
   return ok;
}