From 65a5e3c10200c154cdcd81bc8e99accd4df98acb Mon Sep 17 00:00:00 2001 From: Benedek Thaler Date: Tue, 16 Jul 2024 17:26:53 +0200 Subject: [PATCH] Increase float precision when printing To avoid scientific notation. Fixes #175 --- include/binlog/detail/OstreamBuffer.cpp | 4 ++-- test/integration/LoggingFundamentals.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/binlog/detail/OstreamBuffer.cpp b/include/binlog/detail/OstreamBuffer.cpp index 305e78c..7f032df 100644 --- a/include/binlog/detail/OstreamBuffer.cpp +++ b/include/binlog/detail/OstreamBuffer.cpp @@ -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; } diff --git a/test/integration/LoggingFundamentals.cpp b/test/integration/LoggingFundamentals.cpp index 7d2e09c..1644835 100644 --- a/test/integration/LoggingFundamentals.cpp +++ b/test/integration/LoggingFundamentals.cpp @@ -21,6 +21,13 @@ void logUnsigned() BINLOG_INFO("{} {} {} {}", a, b, c, T(40)); } +template +void logDouble() +{ + T f = 1234234.0234242; + BINLOG_INFO("{}", f); +} + int main() { logSigned(); // NOLINT @@ -66,6 +73,11 @@ int main() // Outputs: -20 30 30 -40 // Outputs: -20 30 30 -40 + logDouble(); + logDouble(); + // Outputs: 1234234.0234242 + // Outputs: 1234234.0234242 + char c = 'A'; const char cc = 'B'; const char& ccr = 'C';