Skip to content

Commit 6c15ce7

Browse files
committed
snprintf/abseil must output 17 digits
- By default, they only output 6 significant digits. - They don't support automatic minimal string repr. - To ensure round-trip validity, we use format %.17g.
1 parent c852119 commit 6c15ce7

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

benchmarks/algorithms.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define ALGORITHMS_H
33

44
#ifndef __CYGWIN__
5-
#include "absl/strings/str_cat.h"
5+
#include "absl/strings/str_format.h"
66
#endif
77

88
#if ERROL_SUPPORTED
@@ -155,7 +155,7 @@ int netlib(T d, std::span<char>& buffer) {
155155

156156
template<arithmetic_float T>
157157
int snprintf(T d, std::span<char>& buffer) {
158-
return std::snprintf(buffer.data(), buffer.size(), "%g", d);
158+
return std::snprintf(buffer.data(), buffer.size(), "%.17g", d);
159159
}
160160

161161
// grisu2::dtoa_impl::grisu2 can take a template type
@@ -219,11 +219,12 @@ int double_conversion(T d, std::span<char>& buffer) {
219219

220220
template<arithmetic_float T>
221221
int abseil(T d, std::span<char>& buffer) {
222-
std::string s;
223-
absl::StrAppend(&s, d);
224-
std::copy(s.begin(), s.end(), buffer.begin());
225-
return size(s);
226-
// return absl::SNPrintF(buffer.data(), buffer.size(), "%g", d);
222+
// StrAppend is faster but only outputs 6 digits after the decimal point
223+
// std::string s;
224+
// absl::StrAppend(&s, d);
225+
// std::copy(s.begin(), s.end(), buffer.begin());
226+
// return size(s);
227+
return absl::SNPrintF(buffer.data(), buffer.size(), "%.17g", d);
227228
}
228229

229230
template<arithmetic_float T>

0 commit comments

Comments
 (0)