Prev Next to_string.cpp Headings

@(@\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@ This is cppad-20221105 documentation. Here is a link to its current documentation .
to_string: Example and Test

// 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;
}

Input File: example/utility/to_string.cpp