44
44
#define YY_DOUBLE_SUPPORTED 0
45
45
#endif
46
46
47
-
48
47
template <arithmetic_float T>
49
48
struct BenchArgs {
50
49
using Type = T;
@@ -63,60 +62,61 @@ struct BenchArgs {
63
62
64
63
namespace BenchmarkShortest {
65
64
66
-
67
65
/* *
68
66
* We have that std::to_chars does not produce the shortest
69
67
* representation for numbers in scientific notation, so we
70
68
* optimize the string representation to be shorter.
71
69
*/
72
70
inline std::string optimize_number_string (const std::string &input) {
73
71
// Check if input contains 'E' or 'e' for scientific notation
74
- auto e_pos = input.find_first_of (" Ee" );
75
- if ( e_pos != std::string::npos) {
72
+ if ( const auto e_pos = input.find_first_of (" Ee" );
73
+ e_pos != std::string::npos) {
76
74
// Handle scientific notation
77
- std::string mantissa = input.substr (0 , e_pos);
75
+ const std::string mantissa = input.substr (0 , e_pos);
78
76
std::string exponent = input.substr (e_pos + 1 );
79
77
80
78
// Remove leading zeros in exponent, preserving sign
81
- bool negative = exponent[0 ] == ' -' ;
82
- exponent.erase (0 , negative ? 1 : 0 );
79
+ const bool negative = exponent[0 ] == ' -' ;
80
+ const bool positive = exponent[0 ] == ' +' ;
81
+ exponent.erase (0 , (negative || positive) ? 1 : 0 );
83
82
exponent.erase (0 , exponent.find_first_not_of (' 0' ));
84
83
if (exponent.empty ())
85
84
exponent = " 0" ;
86
85
if (negative && exponent != " 0" )
87
86
exponent = " -" + exponent;
88
87
89
88
// Reconstruct the number
90
- return mantissa + " E " + exponent;
89
+ return mantissa + " e " + exponent;
91
90
}
92
91
93
92
// Handle non-scientific notation
94
93
if (input == " 0" || input == " -0" )
95
94
return input;
96
95
97
96
// Determine sign
98
- bool is_negative = input[0 ] == ' -' ;
99
- std::string num = is_negative ? input.substr (1 ) : input;
97
+ const bool is_negative = input[0 ] == ' -' ;
100
98
101
99
// Find first and last significant digits
102
- std::string digits = num ;
103
- size_t decimal_pos = digits.find (' .' );
104
- if ( decimal_pos != std::string::npos) {
105
- digits.erase (decimal_pos, 1 ); // Remove decimal point
100
+ std::string digits = is_negative ? input. substr ( 1 ) : input ;
101
+ if ( const size_t decimal_pos = digits.find (' .' );
102
+ decimal_pos != std::string::npos) {
103
+ digits.erase (decimal_pos, 1 ); // Remove decimal point
106
104
}
107
- size_t first_non_zero = digits.find_first_not_of (' 0' );
108
- size_t last_non_zero = digits.find_last_not_of (' 0' );
105
+ const size_t first_non_zero = digits.find_first_not_of (' 0' );
106
+ const size_t last_non_zero = digits.find_last_not_of (' 0' );
109
107
digits = digits.substr (first_non_zero, last_non_zero - first_non_zero + 1 );
108
+
110
109
// Count significant digits
111
- size_t num_digits = digits.length ();
110
+ const size_t num_digits = digits.length ();
112
111
if (num_digits == 0 )
113
112
return input;
113
+
114
114
// Calculate exponent
115
- size_t input_decimal_pos = input.find (' .' );
116
- size_t input_first_non_zero = input.find_first_not_of (' 0' );
117
- size_t input_last_non_zero = input.find_last_not_of (' 0' );
115
+ const size_t input_decimal_pos = input.find (' .' );
116
+ const size_t input_first_non_zero = input.find_first_not_of (' 0' );
117
+ const size_t input_last_non_zero = input.find_last_not_of (' 0' );
118
118
119
- int exponent = 0 ;
119
+ int exponent;
120
120
if (input_decimal_pos == std::string::npos) {
121
121
// we have 123232900000
122
122
exponent = (input_last_non_zero - input_first_non_zero);
@@ -126,19 +126,21 @@ inline std::string optimize_number_string(const std::string &input) {
126
126
} else {
127
127
// Number like 0.000123
128
128
exponent =
129
- -static_cast <int >(input.find_first_not_of (' 0' , input_decimal_pos + 1 ) -
130
- input_decimal_pos);
129
+ -static_cast <int >(input.find_first_not_of (' 0' , input_decimal_pos + 1 )
130
+ - input_decimal_pos);
131
131
}
132
132
// Calculate scientific notation length
133
- size_t mantissa_len =
134
- num_digits + (num_digits > 1 ? 1 : 0 ); // Digits + optional decimal
135
- size_t exponent_len = (exponent == 0 ) ? 1
136
- : (exponent < 0 ? 1 : 0 ) +
137
- (std::abs (exponent) < 10 ? 1
138
- : std::abs (exponent) < 100 ? 2
139
- : 3 );
140
- size_t sci_len = mantissa_len + 1 + exponent_len +
141
- (is_negative ? 1 : 0 ); // Mantissa + E + exponent + sign
133
+ const size_t mantissa_len =
134
+ num_digits + (num_digits > 1 ? 1 : 0 ); // Digits + optional decimal
135
+ const size_t exponent_len = (exponent == 0 )
136
+ ? 1
137
+ : (exponent < 0 ? 1 : 0 )
138
+ + (std::abs (exponent) < 10 ? 1
139
+ : std::abs (exponent) < 100 ? 2
140
+ : 3 );
141
+ const size_t sci_len =
142
+ mantissa_len + 1 + exponent_len
143
+ + (is_negative ? 1 : 0 ); // Mantissa + E + exponent + sign
142
144
143
145
// Compare lengths
144
146
if (sci_len >= input.length ())
@@ -552,7 +554,6 @@ int std_to_chars(T d, std::span<char>& buffer) {
552
554
#endif
553
555
}
554
556
555
-
556
557
} // namespace BenchmarksShortest
557
558
558
559
template <typename T>
0 commit comments