From 47ed6aca2eebab79d28d615c1a28ca386e40c449 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Thu, 22 Aug 2013 12:38:19 +0000 Subject: [PATCH] Fix some buglets in decimal rounding. [SVN r85420] --- .../boost/multiprecision/detail/float_string_cvt.hpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/boost/multiprecision/detail/float_string_cvt.hpp b/include/boost/multiprecision/detail/float_string_cvt.hpp index 622976ab..6e327788 100644 --- a/include/boost/multiprecision/detail/float_string_cvt.hpp +++ b/include/boost/multiprecision/detail/float_string_cvt.hpp @@ -16,7 +16,7 @@ namespace boost{ namespace multiprecision{ namespace detail{ -inline void round_string_up_at(std::string& s, int pos) +inline void round_string_up_at(std::string& s, int pos, int& expon) { // // Rounds up a string representation of a number at pos: @@ -24,14 +24,18 @@ inline void round_string_up_at(std::string& s, int pos) if(pos < 0) { s.insert(0, 1, '1'); + s.erase(s.size() - 1); + ++expon; } else if(s[pos] == '9') { s[pos] = '0'; - round_string_up_at(s, pos - 1); + round_string_up_at(s, pos - 1, expon); } else { + if((pos == 0) && (s[pos] == '0') && (s.size() == 1)) + ++expon; ++s[pos]; } } @@ -152,12 +156,12 @@ std::string convert_to_string(Backend b, std::streamsize digits, std::ios_base:: // Bankers rounding: if((*result.rbegin() - '0') & 1) { - round_string_up_at(result, result.size() - 1); + round_string_up_at(result, result.size() - 1, expon); } } else if(cdigit >= 5) { - round_string_up_at(result, result.size() - 1); + round_string_up_at(result, result.size() - 1, expon); } } }