Skip to content

Commit ffb6adc

Browse files
committed
do not count trailing zeros digits
1 parent 9694457 commit ffb6adc

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

benchmarks/exhaustivefloat32.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,23 @@
1111

1212
size_t count_significant_digits(std::string_view num_str) {
1313
size_t count = 0;
14-
bool has_decimal = false;
15-
bool in_exponent = false;
14+
size_t trailing_zeros = 0;
1615
bool leading_zero = true;
1716

1817
for (char c : num_str) {
19-
if (c == '.') {
20-
has_decimal = true;
18+
if (c == '.')
2119
continue;
22-
}
23-
if (c == 'e' || c == 'E') {
24-
in_exponent = true;
25-
continue;
26-
}
20+
if (c == 'e' || c == 'E')
21+
break; // Stop counting at exponent
2722
if (std::isdigit(static_cast<unsigned char>(c))) {
28-
if (!in_exponent) {
29-
if (leading_zero && c == '0') {
30-
// Skip leading zeros before decimal
31-
continue;
32-
}
33-
leading_zero = false;
34-
count++;
23+
if (c == '0') {
24+
if (!leading_zero)
25+
trailing_zeros++;
26+
continue;
3527
}
36-
}
37-
}
38-
39-
// Special case: "X.0" should count as 1 digit
40-
if (has_decimal && count > 1) {
41-
auto last_digit_pos = num_str.find_last_not_of("0eE+-");
42-
if (last_digit_pos != std::string_view::npos &&
43-
num_str[last_digit_pos] == '.' && count == 2) {
44-
return 1;
28+
leading_zero = false;
29+
count += trailing_zeros + 1;
30+
trailing_zeros = 0;
4531
}
4632
}
4733

@@ -68,6 +54,7 @@ std::string float_to_hex(float f) {
6854
// Convert to hex format
6955
return fmt::format("0x1.{:06x}p{:+d}", mantissa_bits, exponent - 23);
7056
}
57+
7158
void run_exhaustive32(bool errol) {
7259
constexpr auto precision = std::numeric_limits<float>::digits10;
7360
fmt::println("{:20} {:20}", "Algorithm", "Valid shortest serialization");

0 commit comments

Comments
 (0)