Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase float precision when printing #176

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/binlog/detail/OstreamBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ OstreamBuffer& OstreamBuffer::operator<<(bool b)
OstreamBuffer& OstreamBuffer::operator<<(double v)
{
reserve(64);
_p += snprintf(_p, 64, "%g", v);
_p += snprintf(_p, 64, "%.16g", v);
return *this;
}

OstreamBuffer& OstreamBuffer::operator<<(long double v)
{
reserve(128);
_p += snprintf(_p, 128, "%Lg", v);
_p += snprintf(_p, 128, "%.16Lg", v);
return *this;
}

Expand Down
12 changes: 12 additions & 0 deletions test/integration/LoggingFundamentals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ void logUnsigned()
BINLOG_INFO("{} {} {} {}", a, b, c, T(40));
}

template <typename T>
void logDouble()
{
T f = 1234234.0234242;
BINLOG_INFO("{}", f);
}

int main()
{
logSigned<signed short>(); // NOLINT
Expand Down Expand Up @@ -66,6 +73,11 @@ int main()
// Outputs: -20 30 30 -40
// Outputs: -20 30 30 -40

logDouble<double>();
logDouble<long double>();
// Outputs: 1234234.0234242
// Outputs: 1234234.0234242

char c = 'A';
const char cc = 'B';
const char& ccr = 'C';
Expand Down
10 changes: 4 additions & 6 deletions test/unit/binlog/detail/TestOstreamBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,16 @@ TEST_CASE_FIXTURE(TestcaseBase, "shift_op")

CHECK(toString<float>(0.0f) == "0");
CHECK(toString<float>(1.0f) == "1");
CHECK(toString<float>(1.2f) == "1.2");
CHECK(toString<float>(123.456f) == "123.456");
CHECK(toString<float>(120.5625f) == "120.5625");

CHECK(toString<double>(0.0) == "0");
CHECK(toString<double>(1.0) == "1");
CHECK(toString<double>(-1.2) == "-1.2");
CHECK(toString<double>(123.456) == "123.456");
CHECK(toString<double>(120.5625) == "120.5625");

CHECK(toString<long double>(0.0) == "0");
CHECK(toString<long double>(1.0) == "1");
CHECK(toString<long double>(1.2) == "1.2");
CHECK(toString<long double>(-123.456) == "-123.456");
CHECK(toString<long double>(-120.5625) == "-120.5625");
CHECK(toString<long double>(1234234.0234242) == "1234234.0234242");

CHECK(toString("foobar") == "foobar");
}
Loading