Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75602 - sandbox/big_number/boost/multiprecision
From: john_at_[hidden]
Date: 2011-11-22 04:52:00


Author: johnmaddock
Date: 2011-11-22 04:51:58 EST (Tue, 22 Nov 2011)
New Revision: 75602
URL: http://svn.boost.org/trac/boost/changeset/75602

Log:
Change number formatting when std::ios_base::fixed is set to print the requested number of digits, padding with zeros as required.
Text files modified:
   sandbox/big_number/boost/multiprecision/cpp_float.hpp | 8 ++++++++
   sandbox/big_number/boost/multiprecision/gmp.hpp | 27 ++++++++++++++++++---------
   sandbox/big_number/boost/multiprecision/mpfr.hpp | 32 +++++++++++++++++++-------------
   3 files changed, 45 insertions(+), 22 deletions(-)

Modified: sandbox/big_number/boost/multiprecision/cpp_float.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/cpp_float.hpp (original)
+++ sandbox/big_number/boost/multiprecision/cpp_float.hpp 2011-11-22 04:51:58 EST (Tue, 22 Nov 2011)
@@ -1755,6 +1755,14 @@
       str.append(1, 'e');
       str.append(boost::lexical_cast<std::string>(my_exp));
    }
+ if(showpoint || scientific)
+ {
+ std::streamsize chars = str.size() - 1;
+ BOOST_ASSERT(str.find('.') != std::string::npos); // there must be a decimal point!!
+ chars = number_of_digits - chars;
+ if(chars > 0)
+ str.append(chars, '0');
+ }
    if(isneg())
       str.insert(0, 1, '-');
    else if(shopos)

Modified: sandbox/big_number/boost/multiprecision/gmp.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/gmp.hpp (original)
+++ sandbox/big_number/boost/multiprecision/gmp.hpp 2011-11-22 04:51:58 EST (Tue, 22 Nov 2011)
@@ -173,17 +173,14 @@
       void (*free_func_ptr) (void *, size_t);
       const char* ps = mpf_get_str (0, &e, 10, static_cast<std::size_t>(digits), m_data);
       std::ptrdiff_t sl = std::strlen(ps);
+ if(ps && *ps == '-')
+ --sl; // number of digits excluding sign.
+ result = ps;
       if(sl == 0)
       {
          result = scientific ? "0.0e0" : showpoint ? "0.0" : "0";
- if(showpos)
- result.insert(0, 1, '+');
- return "0";
       }
- if(*ps == '-')
- --sl; // number of digits excluding sign.
- result = ps;
- if(fixed || (!scientific && (e > -4) && (e <= std::numeric_limits<boost::uintmax_t>::digits10 + 2)))
+ else if(fixed || (!scientific && (e > -4) && (e <= std::numeric_limits<boost::uintmax_t>::digits10 + 2)))
       {
          if(1 + e >= sl)
          {
@@ -215,8 +212,20 @@
          if(e)
             result += "e" + lexical_cast<std::string>(e);
       }
- if(shopos && (str[0] != '-'))
- str.insert(0, 1, '+');
+ if(showpoint || scientific)
+ {
+ // Pad out end with zeros as required to give required precision.
+ std::streamsize chars = result.size() - 1;
+ BOOST_ASSERT(result.find('.') != std::string::npos); // there must be a decimal point!!
+ BOOST_ASSERT(result.size()); // Better not be a null string by this point!!
+ if(result[0] == '-')
+ --chars;
+ chars = digits - chars;
+ if(chars > 0)
+ result.append(static_cast<std::string::size_type>(chars), '0');
+ }
+ if(showpos && (result[0] != '-'))
+ result.insert(0, 1, '+');
       mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
       (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
       return result;

Modified: sandbox/big_number/boost/multiprecision/mpfr.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/mpfr.hpp (original)
+++ sandbox/big_number/boost/multiprecision/mpfr.hpp 2011-11-22 04:51:58 EST (Tue, 22 Nov 2011)
@@ -168,21 +168,15 @@
       char* ps = mpfr_get_str (0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
       std::ptrdiff_t sl = std::strlen(ps);
       int chars = sl;
- if(sl == 0)
- {
- result = scientific ? "0.0e0" : showpoint ? "0.0" : "0";
- if(showpos)
- result.insert(0, 1, '+');
- return "0";
- }
- while(ps[chars-1] == '0')
+ while(chars && (ps[chars-1] == '0'))
          --chars;
       ps[chars] = 0;
- if(*ps == '-')
+ if(chars && (*ps == '-'))
          --chars; // number of digits excluding sign.
       if(chars == 0)
- return scientific ? "0.0e0" : showpoint ? "0.0" : "0";
- result = ps;
+ result = "0";
+ else
+ result = ps;
       if(fixed || (!scientific && (e > -4) && (e <= std::numeric_limits<boost::uintmax_t>::digits10 + 2)))
       {
          if(e >= chars)
@@ -215,8 +209,20 @@
          if(e)
             result += "e" + lexical_cast<std::string>(e);
       }
- if(shopos && (str[0] != '-'))
- str.insert(0, 1, '+');
+ if(showpoint || scientific)
+ {
+ // Pad out end with zeros as required to give required precision.
+ std::streamsize chars = result.size() - 1;
+ BOOST_ASSERT(result.find('.') != std::string::npos); // there must be a decimal point!!
+ BOOST_ASSERT(result.size()); // Better not be a null string by this point!!
+ if(result[0] == '-')
+ --chars;
+ chars = digits - chars;
+ if(chars > 0)
+ result.append(static_cast<std::string::size_type>(chars), '0');
+ }
+ if(showpos && (result[0] != '-'))
+ result.insert(0, 1, '+');
       mpfr_free_str(ps);
       return result;
    }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk