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 (51 lines) | stat: -rw-r--r-- 1,464 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
// 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
// ----------------------------------------------------------------------------

// Complex examples should suppress conversion warnings
# include <cppad/wno_conversion.hpp>
//
# include <cppad/cppad.hpp>

namespace {
   template <class Float>
   bool test(void)
   {  bool  ok  = true;
      Float eps = std::numeric_limits<Float>::epsilon();
      Float pi  = Float( 4.0 * std::atan(1.0) );
      Float e   = Float( std::exp(1.0) );
      typedef std::complex<Float> Base;
      Base  c   = Base(pi, e);
      std::string s = CppAD::to_string( CppAD::AD<Base>(c) );
      //
      // get strings corresponding to real and imaginary parts
      std::string real = "";
      std::string imag = "";
      size_t index = 1; // skip ( at front
      while( s[index] != ',' )
         real += s[index++];
      index++;
      while( s[index] != ')' )
         imag += s[index++];
      //
      Float check   = Float( std::atof( real.c_str() ) );
      ok           &= std::fabs( check / pi - 1.0 ) <= 2.0 * eps;
      //
      check         = Float( std::atof( imag.c_str() ) );
      ok           &= std::fabs( check / e - 1.0 ) <= 2.0 * eps;
      //
      return ok;
   }
}

bool to_string(void)
{  bool ok = true;

   ok &= test<float>();
   ok &= test<double>();

   return ok;
}

// END C++