File: to_string.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 (128 lines) | stat: -rw-r--r-- 3,496 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
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
// 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
// ----------------------------------------------------------------------------

/*
{xrst_begin to_string.cpp}

to_string: Example and Test
###########################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end to_string.cpp}
*/
// BEGIN C++

// Examples with fundamental types
# include <cppad/utility/to_string.hpp>
namespace {
   template <class Integer>
   Integer string2signed(const std::string& s)
   {  Integer result = 0;
      size_t index   = 0;
      if( s[0] == '-' )
      {  ++index;
         while( index < s.size() )
            result = Integer(10) * result - Integer(s[index++] - '0' );
      }
      else
      {  while( index < s.size() )
            result = Integer(10) * result + Integer(s[index++] - '0' );
      }
      return result;
   }
   template <class Integer>
   Integer string2unsigned(const std::string& s)
   {  Integer result = 0;
      size_t index   = 0;
      while( index < s.size() )
         result = Integer(10 * result + Integer(s[index++]) - '0');
      return result;
   }
   template <class Integer>
   bool signed_integer(void)
   {  bool ok = true;
      //
      Integer max    = std::numeric_limits<Integer>::max();
      std::string s  = CppAD::to_string(max);
      Integer check  = string2signed<Integer>(s);
      ok            &= max == check;
      //
      Integer min    = std::numeric_limits<Integer>::min();
      s              = CppAD::to_string(min);
      check          = string2signed<Integer>(s);
      ok            &= min == check;
      //
      return ok;
   }
   template <class Integer>
   bool unsigned_integer(void)
   {  bool ok = true;
      //
      Integer max    = std::numeric_limits<Integer>::max();
      std::string s  = CppAD::to_string(max);
      Integer check  = string2unsigned<Integer>(s);
      ok            &= max == check;
      ok            &= std::numeric_limits<Integer>::min() == 0;
      //
      return ok;
   }
   template <class Float>
   bool floating(void)
   {  bool  ok  = true;
      Float eps = std::numeric_limits<Float>::epsilon();
      Float pi  = Float( 4.0 * std::atan(1.0) );
      //
      std::string s = CppAD::to_string( pi );
      Float check   = Float( std::atof( s.c_str() ) );
      ok           &= std::fabs( check / pi - 1.0 ) <= 2.0 * eps;
      //
      return ok;
   }
}

// Examples with AD types
# include <cppad/cppad.hpp>
namespace {
   template <class Base>
   bool ad_floating(void)
   {  bool  ok  = true;
      Base eps  = std::numeric_limits<Base>::epsilon();
      Base pi   = Base( 4.0 * std::atan(1.0) );
      //
      std::string s = CppAD::to_string( CppAD::AD<Base>( pi ) );
      Base check    = Base( std::atof( s.c_str() ) );
      ok           &= fabs( check / pi - Base(1.0) ) <= Base( 2.0 ) * eps;
      //
      return ok;
   }
}

// Test driver
bool to_string(void)
{  bool ok = true;

   ok &= unsigned_integer<unsigned short>();
   ok &= signed_integer<signed int>();
   //
   ok &= unsigned_integer<unsigned long>();
   ok &= signed_integer<signed long>();
   ok &= unsigned_integer<unsigned long long>();
   ok &= signed_integer<signed long long>();
   //
   ok &= floating<float>();
   ok &= floating<double>();
   ok &= floating<long double>();
   //
   ok &= ad_floating<float>();
   ok &= ad_floating<double>();
   //
   return ok;
}

// END C++