mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 07:31:57 +00:00
dbms: harness the power of libdouble-conversion. [#METR-13363]
This commit is contained in:
parent
3be8b25ed2
commit
00db574116
@ -17,6 +17,8 @@
|
||||
#include <DB/IO/WriteHelpers.h>
|
||||
#include <DB/IO/WriteBufferFromString.h>
|
||||
|
||||
#include <DB/IO/DoubleConverter.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
@ -590,24 +592,17 @@ private:
|
||||
*
|
||||
* NOTE: При таком roundtrip-е, точность может теряться.
|
||||
*/
|
||||
static inline String formatFloat(Float64 x)
|
||||
static String formatFloat(const Float64 x)
|
||||
{
|
||||
char tmp[24];
|
||||
int res = std::snprintf(tmp, 23, "%.*g", WRITE_HELPERS_DEFAULT_FLOAT_PRECISION, x);
|
||||
char tmp[25];
|
||||
double_conversion::StringBuilder builder{tmp, sizeof(tmp)};
|
||||
|
||||
if (res >= 23 || res <= 0)
|
||||
const auto result = getDoubleToStringConverter().ToShortest(x, &builder);
|
||||
|
||||
if (!result)
|
||||
throw Exception("Cannot print float or double number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
|
||||
|
||||
size_t string_size = res;
|
||||
|
||||
tmp[23] = '\0';
|
||||
if (string_size == strspn(tmp, "-0123456789"))
|
||||
{
|
||||
tmp[string_size] = '.';
|
||||
++string_size;
|
||||
}
|
||||
|
||||
return {tmp, string_size};
|
||||
return { tmp, tmp + builder.position() };
|
||||
}
|
||||
|
||||
public:
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <DB/IO/WriteIntText.h>
|
||||
#include <DB/IO/VarInt.h>
|
||||
#include <DB/IO/WriteBufferFromString.h>
|
||||
#include <DB/IO/DoubleConverter.h>
|
||||
#include <city.h>
|
||||
|
||||
#define WRITE_HELPERS_DEFAULT_FLOAT_PRECISION 6U
|
||||
@ -89,13 +90,15 @@ inline void writeBoolText(bool x, WriteBuffer & buf)
|
||||
template <typename T>
|
||||
void writeFloatText(T x, WriteBuffer & buf, unsigned precision = WRITE_HELPERS_DEFAULT_FLOAT_PRECISION)
|
||||
{
|
||||
char tmp[24];
|
||||
int res = std::snprintf(tmp, 24, "%.*g", precision, x);
|
||||
char tmp[25];
|
||||
double_conversion::StringBuilder builder{tmp, sizeof(tmp)};
|
||||
|
||||
if (res >= 24 || res <= 0)
|
||||
const auto result = getDoubleToStringConverter<false>().ToShortest(x, &builder);
|
||||
|
||||
if (!result)
|
||||
throw Exception("Cannot print float or double number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
|
||||
|
||||
buf.write(tmp, res);
|
||||
buf.write(tmp, builder.position());
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user