1414
1515#include " algorithms.h"
1616#include " cxxopts.hpp"
17-
18- size_t count_significant_digits (std::string_view num_str) {
19- size_t count = 0 ;
20- size_t trailing_zeros = 0 ;
21- bool leading_zero = true ;
22-
23- for (char c : num_str) {
24- if (c == ' .' )
25- continue ;
26- if (c == ' e' || c == ' E' )
27- break ; // Stop counting at exponent
28- if (std::isdigit (static_cast <unsigned char >(c))) {
29- if (c == ' 0' ) {
30- if (!leading_zero)
31- trailing_zeros++;
32- continue ;
33- }
34- leading_zero = false ;
35- count += trailing_zeros + 1 ;
36- trailing_zeros = 0 ;
37- }
38- }
39-
40- return count;
41- }
42-
43- std::string double_to_hex (double d) {
44- std::ostringstream oss;
45- oss << std::hexfloat << d;
46- return oss.str ();
47- }
48-
49- std::optional<double > parse_double (std::string_view sv) {
50- double result;
51- const char * begin = sv.data ();
52- const char * end = sv.data () + sv.size ();
53-
54- auto [ptr, ec] = std::from_chars (begin, end, result);
55-
56- // Check if parsing succeeded and consumed the entire string
57- if (ec == std::errc{} && ptr == end) {
58- return result;
59- }
60-
61- // Return nullopt if parsing failed or didn't consume all input
62- return std::nullopt ;
63- }
17+ #include " floatutils.h"
6418
6519struct test_case {
6620 double value;
@@ -71,27 +25,24 @@ struct test_case {
7125std::vector<test_case> load_doubles_from_file (const std::string& filename) {
7226 std::vector<test_case> numbers;
7327 std::ifstream file (filename);
74- std::string line;
7528
7629 if (!file.is_open ()) {
7730 fmt::print (" Error: Could not open file {}\n " , filename);
7831 return numbers;
7932 }
8033
81- while (std::getline (file, line)) {
82- if (auto num = parse_double (line)) {
34+ for (std::string line; std:: getline (file, line); ) {
35+ if (auto num = parse_float< double > (line))
8336 numbers.emplace_back (*num,line);
84- } else {
37+ else
8538 fmt::print (" Warning: Could not parse '{}' as double, skipping\n " , line);
86- }
8739 }
8840
8941 file.close ();
9042 return numbers;
9143}
9244
9345void run_file_test (const std::string& filename, bool errol, const std::vector<std::string>& algo_filter = {}) {
94- constexpr auto precision = std::numeric_limits<double >::digits10;
9546 fmt::println (" {:20} {:20}" , " Algorithm" , " Valid shortest serialization" );
9647
9748 std::array<Benchmarks::BenchArgs<double >, Benchmarks::COUNT> args;
@@ -155,13 +106,13 @@ void run_file_test(const std::string& filename, bool errol, const std::vector<st
155106
156107 auto countRef = count_significant_digits (svRef);
157108 auto countAlgo = count_significant_digits (svAlgo);
158- auto backRef = parse_double (svRef);
159- auto backAlgo = parse_double (svAlgo);
109+ auto backRef = parse_float< double > (svRef);
110+ auto backAlgo = parse_float< double > (svAlgo);
160111
161112 if (!backRef || !backAlgo) {
162113 incorrect = true ;
163- fmt::print (" parse error: case: {}; d = {}, bufRef = {}, bufAlgo = {}" , str_value, double_to_hex (d),
164- svRef, svAlgo);
114+ fmt::print (" parse error: case: {}; d = {}, bufRef = {}, bufAlgo = {}" ,
115+ str_value, float_to_hex< double >(d), svRef, svAlgo);
165116 fflush (stdout);
166117 break ;
167118 }
@@ -170,20 +121,23 @@ void run_file_test(const std::string& filename, bool errol, const std::vector<st
170121 }
171122 if (*backRef != d) {
172123 incorrect = true ;
173- fmt::print (" ref mismatch:case: {}; d = {}, backRef = {}; svRef = {}, svAlgo = {}" , str_value, double_to_hex (d), *backRef, svRef, svAlgo);
124+ fmt::print (" ref mismatch:case: {}; d = {}, backRef = {}; svRef = {}, svAlgo = {}" ,
125+ str_value, float_to_hex<double >(d), *backRef, svRef, svAlgo);
174126 fflush (stdout);
175127 break ;
176128 }
177129 if (*backAlgo != d) {
178130 incorrect = true ;
179- fmt::print (" algo mismatch: case: {}; d = {}, backAlgo = {}; svRef = {}, svAlgo = {}, parsing the output with std::from_chars does not recover the original" , str_value, double_to_hex (d), *backAlgo, svRef, svAlgo);
131+ fmt::print (" algo mismatch: case: {}; d = {}, backAlgo = {}; svRef = {}, svAlgo = {}, "
132+ " parsing the output with std::from_chars does not recover the original" ,
133+ str_value, float_to_hex<double >(d), *backAlgo, svRef, svAlgo);
180134 fflush (stdout);
181135 break ;
182136 }
183137 if (countRef != countAlgo) {
184138 incorrect = true ;
185- fmt::print (" mismatch: case: {}; d = {}, bufRef = {}, bufAlgo = {}" , str_value, double_to_hex (d),
186- svRef, svAlgo);
139+ fmt::print (" mismatch: case: {}; d = {}, bufRef = {}, bufAlgo = {}" ,
140+ str_value, float_to_hex< double >(d), svRef, svAlgo);
187141 fflush (stdout);
188142 break ;
189143 }
@@ -222,4 +176,4 @@ int main(int argc, char **argv) {
222176 fmt::print (" error parsing options: {}\n " , e.what ());
223177 return EXIT_FAILURE;
224178 }
225- }
179+ }
0 commit comments