Skip to content

Commit 3f5bc95

Browse files
erenonspektrof
authored andcommitted
Increase float precision when printing
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
1 parent 6e69906 commit 3f5bc95

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

include/binlog/detail/OstreamBuffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ OstreamBuffer& OstreamBuffer::operator<<(bool b)
4747
OstreamBuffer& OstreamBuffer::operator<<(double v)
4848
{
4949
reserve(64);
50-
_p += snprintf(_p, 64, "%g", v);
50+
_p += snprintf(_p, 64, "%.16g", v);
5151
return *this;
5252
}
5353

5454
OstreamBuffer& OstreamBuffer::operator<<(long double v)
5555
{
5656
reserve(128);
57-
_p += snprintf(_p, 128, "%Lg", v);
57+
_p += snprintf(_p, 128, "%.16Lg", v);
5858
return *this;
5959
}
6060

test/integration/LoggingFundamentals.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ void logUnsigned()
2121
BINLOG_INFO("{} {} {} {}", a, b, c, T(40));
2222
}
2323

24+
template <typename T>
25+
void logDouble()
26+
{
27+
T f = 1234234.0234242;
28+
BINLOG_INFO("{}", f);
29+
}
30+
2431
int main()
2532
{
2633
logSigned<signed short>(); // NOLINT
@@ -66,6 +73,11 @@ int main()
6673
// Outputs: -20 30 30 -40
6774
// Outputs: -20 30 30 -40
6875

76+
logDouble<double>();
77+
logDouble<long double>();
78+
// Outputs: 1234234.0234242
79+
// Outputs: 1234234.0234242
80+
6981
char c = 'A';
7082
const char cc = 'B';
7183
const char& ccr = 'C';

test/unit/binlog/detail/TestOstreamBuffer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,16 @@ TEST_CASE_FIXTURE(TestcaseBase, "shift_op")
7070

7171
CHECK(toString<float>(0.0f) == "0");
7272
CHECK(toString<float>(1.0f) == "1");
73-
CHECK(toString<float>(1.2f) == "1.2");
74-
CHECK(toString<float>(123.456f) == "123.456");
73+
CHECK(toString<float>(120.5625f) == "120.5625");
7574

7675
CHECK(toString<double>(0.0) == "0");
7776
CHECK(toString<double>(1.0) == "1");
78-
CHECK(toString<double>(-1.2) == "-1.2");
79-
CHECK(toString<double>(123.456) == "123.456");
77+
CHECK(toString<double>(120.5625) == "120.5625");
8078

8179
CHECK(toString<long double>(0.0) == "0");
8280
CHECK(toString<long double>(1.0) == "1");
83-
CHECK(toString<long double>(1.2) == "1.2");
84-
CHECK(toString<long double>(-123.456) == "-123.456");
81+
CHECK(toString<long double>(-120.5625) == "-120.5625");
82+
CHECK(toString<long double>(1234234.0234242) == "1234234.0234242");
8583

8684
CHECK(toString("foobar") == "foobar");
8785
}

0 commit comments

Comments
 (0)