Replace tostring with stringstream for floating point precision.

This commit is contained in:
Lenard Szolnoki 2017-07-04 00:17:06 +02:00
parent 374e925f67
commit 7b518935b6
2 changed files with 9 additions and 19 deletions

View File

@ -42,19 +42,6 @@ static const std::regex re_float(
"[-+]?(\\.[0-9]+|[0-9]+(\\.[0-9]*)?)([eE][-+]?[0-9]+)?");
static const std::regex re_inf("[-+]?(\\.inf|\\.Inf|\\.INF)");
static const std::regex re_nan("\\.nan|\\.NaN|\\.NAN");
inline bool IsInfinity(const std::string& input) {
return input == ".inf" || input == ".Inf" || input == ".INF" ||
input == "+.inf" || input == "+.Inf" || input == "+.INF";
}
inline bool IsNegativeInfinity(const std::string& input) {
return input == "-.inf" || input == "-.Inf" || input == "-.INF";
}
inline bool IsNaN(const std::string& input) {
return input == ".nan" || input == ".NaN" || input == ".NAN";
}
}
// Node
@ -199,11 +186,14 @@ struct convert<
}
return Node(".inf");
}
auto str = std::to_string(rhs);
std::stringstream stream;
stream.precision(std::numeric_limits<type>::max_digits10);
stream << rhs;
auto str = stream.str();
if (std::regex_match(str, conversion::re_decimal)) {
return Node(str + "."); // Disambiguate float from int
}
return Node(std::to_string(rhs));
return Node(str);
}
static bool decode(const Node& node, type& rhs) {

View File

@ -24,8 +24,8 @@ EmitterState::EmitterState()
m_seqFmt.set(Block);
m_mapFmt.set(Block);
m_mapKeyFmt.set(Auto);
m_floatPrecision.set(std::numeric_limits<float>::digits10 + 1);
m_doublePrecision.set(std::numeric_limits<double>::digits10 + 1);
m_floatPrecision.set(std::numeric_limits<float>::max_digits10);
m_doublePrecision.set(std::numeric_limits<double>::max_digits10);
}
EmitterState::~EmitterState() {}
@ -349,7 +349,7 @@ bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope) {
}
bool EmitterState::SetFloatPrecision(std::size_t value, FmtScope::value scope) {
if (value > std::numeric_limits<float>::digits10 + 1)
if (value > std::numeric_limits<float>::max_digits10)
return false;
_Set(m_floatPrecision, value, scope);
return true;
@ -357,7 +357,7 @@ bool EmitterState::SetFloatPrecision(std::size_t value, FmtScope::value scope) {
bool EmitterState::SetDoublePrecision(std::size_t value,
FmtScope::value scope) {
if (value > std::numeric_limits<double>::digits10 + 1)
if (value > std::numeric_limits<double>::max_digits10)
return false;
_Set(m_doublePrecision, value, scope);
return true;