mirror of
https://github.com/nlohmann/json.git
synced 2025-07-16 18:41:53 +03:00
cleanup after #191
This commit is contained in:
@ -5604,13 +5604,20 @@ class basic_json
|
||||
|
||||
case value_t::number_float:
|
||||
{
|
||||
// If the number is an integer then output as a fixed with with precision 1
|
||||
// to output "0.0", "1.0" etc as expected for some round trip tests otherwise
|
||||
// 15 digits of precision allows round-trip IEEE 754 string->double->string;
|
||||
// to be safe, we read this value from std::numeric_limits<number_float_t>::digits10
|
||||
if (std::fmod(m_value.number_float, 1) == 0) o << std::fixed << std::setprecision(1);
|
||||
else {
|
||||
o.unsetf(std::ios_base::floatfield); // std::defaultfloat not supported in gcc version < 5
|
||||
// If the number is an integer then output as a fixed with with
|
||||
// precision 1 to output "0.0", "1.0" etc as expected for some
|
||||
// round trip tests otherwise 15 digits of precision allows
|
||||
// round-trip IEEE 754 string->double->string; to be safe, we
|
||||
// read this value from
|
||||
// std::numeric_limits<number_float_t>::digits10
|
||||
if (std::fmod(m_value.number_float, 1) == 0)
|
||||
{
|
||||
o << std::fixed << std::setprecision(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// std::defaultfloat not supported in gcc version < 5
|
||||
o.unsetf(std::ios_base::floatfield);
|
||||
o << std::setprecision(std::numeric_limits<double>::digits10);
|
||||
}
|
||||
o << m_value.number_float;
|
||||
@ -7046,55 +7053,35 @@ class basic_json
|
||||
@brief parse floating point number
|
||||
|
||||
This function (and its overloads) serves to select the most approprate
|
||||
standard floating point number parsing function based on the type
|
||||
supplied via the first parameter. Set this to
|
||||
@a static_cast<number_float_t>(nullptr).
|
||||
standard floating point number parsing function (i.e., `std::strtof`,
|
||||
`std::strtod`, or `std::strtold`) based on the type supplied via the
|
||||
first parameter. Set this to @a static_cast<number_float_t>(nullptr).
|
||||
|
||||
@param type the @ref number_float_t in use
|
||||
@param[in] type the @ref number_float_t in use
|
||||
|
||||
@param endptr recieves a pointer to the first character after the number
|
||||
@param[in,out] endptr recieves a pointer to the first character after
|
||||
the number
|
||||
|
||||
@return the floating point number
|
||||
|
||||
@warning This function uses `std::strtof`, `std::strtod`, or
|
||||
`std::strtold` which use the current C locale to determine which
|
||||
character is used as decimal point character. This may yield to parse
|
||||
errors if the locale does not used `.`.
|
||||
*/
|
||||
long double str_to_float_t(long double* /* type */, char** endptr) const
|
||||
{
|
||||
return std::strtold(reinterpret_cast<typename string_t::const_pointer>(m_start), endptr);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief parse floating point number
|
||||
|
||||
This function (and its overloads) serves to select the most approprate
|
||||
standard floating point number parsing function based on the type
|
||||
supplied via the first parameter. Set this to
|
||||
@a static_cast<number_float_t>(nullptr).
|
||||
|
||||
@param type the @ref number_float_t in use
|
||||
|
||||
@param endptr recieves a pointer to the first character after the number
|
||||
|
||||
@return the floating point number
|
||||
*/
|
||||
double str_to_float_t(double* /* type */, char** endptr) const
|
||||
/// @copydoc str_to_float_t
|
||||
double str_to_float_t(double*, char** endptr) const
|
||||
{
|
||||
return std::strtod(reinterpret_cast<typename string_t::const_pointer>(m_start), endptr);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief parse floating point number
|
||||
|
||||
This function (and its overloads) serves to select the most approprate
|
||||
standard floating point number parsing function based on the type
|
||||
supplied via the first parameter. Set this to
|
||||
@a static_cast<number_float_t>(nullptr).
|
||||
|
||||
@param type the @ref number_float_t in use
|
||||
|
||||
@param endptr recieves a pointer to the first character after the number
|
||||
|
||||
@return the floating point number
|
||||
*/
|
||||
float str_to_float_t(float* /* type */, char** endptr) const
|
||||
/// @copydoc str_to_float_t
|
||||
float str_to_float_t(float*, char** endptr) const
|
||||
{
|
||||
return std::strtof(reinterpret_cast<typename string_t::const_pointer>(m_start), endptr);
|
||||
}
|
||||
@ -7369,8 +7356,8 @@ class basic_json
|
||||
|
||||
// check if conversion loses precision (special case -0.0 always loses precision)
|
||||
const auto int_val = static_cast<number_integer_t>(result.m_value.number_float);
|
||||
if (result.m_value.number_float == static_cast<number_float_t>(int_val) &&
|
||||
result.m_value.number_integer != json_value(-0.0f).number_integer)
|
||||
if (result.m_value.number_float == static_cast<number_float_t>(int_val) and
|
||||
result.m_value.number_integer != json_value(-0.0f).number_integer)
|
||||
{
|
||||
// we would not lose precision -> return int
|
||||
result.m_type = value_t::number_integer;
|
||||
|
Reference in New Issue
Block a user