1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
3 
4 CppAD is distributed under the terms of the
5              Eclipse Public License Version 2.0.
6 
7 This Source Code may also be made available under the following
8 Secondary License when the conditions for such availability set forth
9 in the Eclipse Public License, Version 2.0 are satisfied:
10       GNU General Public License, Version 2.0 or later.
11 ---------------------------------------------------------------------------- */
12 
13 /*
14 $begin to_string.cpp$$
15 
16 $section to_string: Example and Test$$
17 
18 $srcthisfile%0%// BEGIN C++%// END C++%1%$$
19 
20 $end
21 */
22 // BEGIN C++
23 
24 // Examples with fundamental types
25 # include <cppad/utility/to_string.hpp>
26 namespace {
27     template <class Integer>
string2signed(const std::string & s)28     Integer string2signed(const std::string& s)
29     {   Integer result = 0;
30         size_t index   = 0;
31         if( s[0] == '-' )
32             ++index;
33         while( index < s.size() )
34             result = Integer( 10 * result + (s[index++] - '0') );
35         if( s[0] == '-' )
36             return - result;
37         return result;
38     }
39     template <class Integer>
string2unsigned(const std::string & s)40     Integer string2unsigned(const std::string& s)
41     {   Integer result = 0;
42         size_t index   = 0;
43         while( index < s.size() )
44             result = Integer(10 * result + Integer(s[index++]) - '0');
45         return result;
46     }
47     template <class Integer>
signed_integer(void)48     bool signed_integer(void)
49     {   bool ok = true;
50         //
51         Integer max    = std::numeric_limits<Integer>::max();
52         std::string s  = CppAD::to_string(max);
53         Integer check  = string2signed<Integer>(s);
54         ok            &= max == check;
55         //
56         Integer min    = std::numeric_limits<Integer>::min();
57         s              = CppAD::to_string(min);
58         check          = string2signed<Integer>(s);
59         ok            &= min == check;
60         //
61         return ok;
62     }
63     template <class Integer>
unsigned_integer(void)64     bool unsigned_integer(void)
65     {   bool ok = true;
66         //
67         Integer max    = std::numeric_limits<Integer>::max();
68         std::string s  = CppAD::to_string(max);
69         Integer check  = string2unsigned<Integer>(s);
70         ok            &= max == check;
71         ok            &= std::numeric_limits<Integer>::min() == 0;
72         //
73         return ok;
74     }
75     template <class Float>
floating(void)76     bool floating(void)
77     {   bool  ok  = true;
78         Float eps = std::numeric_limits<Float>::epsilon();
79         Float pi  = Float( 4.0 * std::atan(1.0) );
80         //
81         std::string s = CppAD::to_string( pi );
82         Float check   = Float( std::atof( s.c_str() ) );
83         ok           &= std::fabs( check / pi - 1.0 ) <= 2.0 * eps;
84         //
85         return ok;
86     }
87 }
88 
89 // Examples with AD types
90 # include <cppad/cppad.hpp>
91 namespace {
92     template <class Base>
ad_floating(void)93     bool ad_floating(void)
94     {   bool  ok  = true;
95         Base eps  = std::numeric_limits<Base>::epsilon();
96         Base pi   = Base( 4.0 * std::atan(1.0) );
97         //
98         std::string s = CppAD::to_string( CppAD::AD<Base>( pi ) );
99         Base check    = Base( std::atof( s.c_str() ) );
100         ok           &= fabs( check / pi - Base(1.0) ) <= Base( 2.0 ) * eps;
101         //
102         return ok;
103     }
104 }
105 
106 // Test driver
to_string(void)107 bool to_string(void)
108 {   bool ok = true;
109 
110     ok &= unsigned_integer<unsigned short>();
111     ok &= signed_integer<signed int>();
112     //
113     ok &= unsigned_integer<unsigned long>();
114     ok &= signed_integer<signed long>();
115     ok &= unsigned_integer<unsigned long long>();
116     ok &= signed_integer<signed long long>();
117     //
118     ok &= floating<float>();
119     ok &= floating<double>();
120     ok &= floating<long double>();
121     //
122     ok &= ad_floating<float>();
123     ok &= ad_floating<double>();
124     //
125     return ok;
126 }
127 
128 // END C++
129