Skip to content

Commit c852119

Browse files
committed
fix the output of netlib
It seems with netlib we have to manually build the output string by placing the sign, decimal separator and leading zeroes.
1 parent 2ee0310 commit c852119

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

benchmarks/algorithms.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,33 @@ int netlib(T d, std::span<char>& buffer) {
117117
char* rve;
118118
char* result = dtoa(d, 0, 0, &decpt, &sign, &rve);
119119
if (result) {
120-
const int volume = rve - result;
121-
std::copy(result, rve, buffer.begin());
120+
int i = 0;
121+
if (sign)
122+
buffer[i++] = '-';
123+
if (decpt > 0) {
124+
// Integer part
125+
const int integer_digits = std::min(decpt, static_cast<int>(rve - result));
126+
std::copy_n(result, integer_digits, buffer.data() + i);
127+
i += integer_digits;
128+
} else {
129+
// Number is < 1 (e.g., 0.000123)
130+
buffer[i++] = '0';
131+
buffer[i++] = '.';
132+
std::fill_n(buffer.data() + i, -decpt, '0'); // Add leading zeros
133+
i += -decpt;
134+
}
135+
136+
// Fractional part (if any remaining digits)
137+
const int remaining_digits = rve - (result + std::max(0, decpt));
138+
if (remaining_digits > 0) {
139+
if (decpt > 0)
140+
buffer[i++] = '.';
141+
std::copy_n(result + std::max(0, decpt), remaining_digits, buffer.data() + i);
142+
i += remaining_digits;
143+
}
144+
122145
freedtoa(result);
123-
return volume;
146+
return i;
124147
} else {
125148
std::cerr << "problem with " << d << std::endl;
126149
std::abort();

0 commit comments

Comments
 (0)