Skip to content

Commit

Permalink
Increase float precision when printing
Browse files Browse the repository at this point in the history
To avoid scientific notation.

Do not test printing of numbers that cannot be
represented precisely using IEEE floats,
test x/(2^y) numbers only.

Fixes #175
  • Loading branch information
erenon committed Jul 29, 2024
1 parent 6e69906 commit 73bb6d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
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");
}

0 comments on commit 73bb6d1

Please sign in to comment.