1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-09 11:01:47 +03:00

fixed more float parsing cases

This commit is contained in:
Niels
2015-06-13 12:42:18 +02:00
parent 7c579f11e5
commit bd0cb65b7a
3 changed files with 20 additions and 18 deletions

View File

@ -2283,8 +2283,9 @@ class basic_json
case (value_t::number_float):
{
// 15 digits of precision allows round-trip IEEE 754
// string->double->string
o << std::setprecision(15) << m_value.number_float;
// string->double->string; to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
o << std::setprecision(std::numeric_limits<number_float_t>::digits10) << m_value.number_float;
return;
}
@ -3853,12 +3854,12 @@ class basic_json
@exception std::range_error if passed value is out of range
*/
number_float_t get_number() const
long double get_number() const
{
// conversion
typename string_t::value_type* endptr;
const auto float_val = std::strtod(reinterpret_cast<typename string_t::const_pointer>(m_start),
&endptr);
const auto float_val = std::strtold(reinterpret_cast<typename string_t::const_pointer>(m_start),
&endptr);
// return float_val if the whole number was translated and NAN
// otherwise
@ -4093,7 +4094,7 @@ class basic_json
// check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val);
if (approx(float_val, static_cast<number_float_t>(int_val)))
if (approx(float_val, static_cast<long double>(int_val)))
{
// we basic_json not lose precision -> return int
result.m_type = value_t::number_integer;
@ -4103,7 +4104,7 @@ class basic_json
{
// we would lose precision -> returnfloat
result.m_type = value_t::number_float;
result.m_value = float_val;
result.m_value = static_cast<number_float_t>(float_val);
}
break;
}