Fix for printing long doubles bug in dump_float

When you use long double as a floating point type with the current version of this file and try to dump json it prints trash instead of actual number. This if-else fixes the problem. On using long double you just need to add an 'L' modifier before 'g' in format string.
This commit is contained in:
Kirill Lokotkov 2023-01-25 18:33:40 +03:00 committed by GitHub
parent da6b908c4f
commit 1885f4bc5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -832,7 +832,12 @@ class serializer
// the actual conversion
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
std::ptrdiff_t len;
if constexpr (std::is_same_v<number_float_t, long double>) {
len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*Lg", d, x);
} else {
len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
}
// negative value indicates an error
JSON_ASSERT(len > 0);