Debian Codegraph @

tests/basictest.cpp

raw
1 #define DOCTEST_CONFIG_SUPER_FAST_ASSERTS
2 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
3 #include "doctest/doctest.h"
4
5 #include "fast_float/fast_float.h"
6 #include <cfenv>
7 #include <cmath>
8 #include <cstdio>
9 #include <iomanip>
10 #include <ios>
11 #include <iostream>
12 #include <limits>
13 #include <sstream>
14 #include <string>
15 #include <system_error>
16 #include <type_traits>
17
18 #if FASTFLOAT_IS_CONSTEXPR
19 #ifndef FASTFLOAT_CONSTEXPR_TESTS
20 #define FASTFLOAT_CONSTEXPR_TESTS 1
21 #endif // #ifndef FASTFLOAT_CONSTEXPR_TESTS
22 #endif // FASTFLOAT_IS_CONSTEXPR
23
24 #if FASTFLOAT_HAS_BIT_CAST
25 #include <bit>
26 #endif
27
28 #ifndef SUPPLEMENTAL_TEST_DATA_DIR
29 #define SUPPLEMENTAL_TEST_DATA_DIR "data/"
30 #endif
31
32 #ifndef __cplusplus
33 #error fastfloat requires a C++ compiler
34 #endif
35
36 #ifndef FASTFLOAT_CPLUSPLUS
37 #if defined(_MSVC_LANG) && !defined(__clang__)
38 #define FASTFLOAT_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG)
39 #else
40 #define FASTFLOAT_CPLUSPLUS __cplusplus
41 #endif
42 #endif
43
44 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
45 defined(sun) || defined(__sun)
46 #define FASTFLOAT_ODDPLATFORM 1
47 #endif
48 #if defined __has_include
49 #if __has_include(<filesystem>)
50 #else
51 // filesystem is not available
52 #define FASTFLOAT_ODDPLATFORM 1
53 #endif
54 #else
55 // __has_include is not available
56 #define FASTFLOAT_ODDPLATFORM 1
57 #endif
58
59 template <typename T> std::string iHexAndDec(T v) {
60 std::ostringstream ss;
61 ss << std::hex << "0x" << (v) << " (" << std::dec << (v) << ")";
62 return ss.str();
63 }
64
65 template <typename T> std::string fHexAndDec(T v) {
66 std::ostringstream ss;
67 ss << std::hexfloat << (v) << " (" << std::defaultfloat
68 << std::setprecision(DBL_MAX_10_EXP + 1) << (v) << ")";
69 return ss.str();
70 }
71
72 char const *round_name(int d) {
73 switch (d) {
74 case FE_UPWARD:
75 return "FE_UPWARD";
76 case FE_DOWNWARD:
77 return "FE_DOWNWARD";
78 case FE_TOWARDZERO:
79 return "FE_TOWARDZERO";
80 case FE_TONEAREST:
81 return "FE_TONEAREST";
82 default:
83 return "UNKNOWN";
84 }
85 }
86
87 #define FASTFLOAT_STR(x) #x
88 #define SHOW_DEFINE(x) printf("%s='%s'\n", #x, FASTFLOAT_STR(x))
89
90 TEST_CASE("system_info") {
91 std::cout << "system info:" << std::endl;
92 #ifdef FASTFLOAT_CONSTEXPR_TESTS
93 SHOW_DEFINE(FASTFLOAT_CONSTEXPR_TESTS);
94
95 #endif
96 #ifdef _MSC_VER
97 SHOW_DEFINE(_MSC_VER);
98 #endif
99 #ifdef FASTFLOAT_64BIT_LIMB
100 SHOW_DEFINE(FASTFLOAT_64BIT_LIMB);
101 #endif
102 #ifdef __clang__
103 SHOW_DEFINE(__clang__);
104 #endif
105 #ifdef FASTFLOAT_VISUAL_STUDIO
106 SHOW_DEFINE(FASTFLOAT_VISUAL_STUDIO);
107 #endif
108 #ifdef FASTFLOAT_IS_BIG_ENDIAN
109 #if FASTFLOAT_IS_BIG_ENDIAN
110 printf("big endian\n");
111 #else
112 printf("little endian\n");
113 #endif
114 #endif
115 #ifdef FASTFLOAT_32BIT
116 SHOW_DEFINE(FASTFLOAT_32BIT);
117 #endif
118 #ifdef FASTFLOAT_64BIT
119 SHOW_DEFINE(FASTFLOAT_64BIT);
120 #endif
121 #ifdef FLT_EVAL_METHOD
122 SHOW_DEFINE(FLT_EVAL_METHOD);
123 #endif
124 #ifdef _WIN32
125 SHOW_DEFINE(_WIN32);
126 #endif
127 #ifdef _WIN64
128 SHOW_DEFINE(_WIN64);
129 #endif
130 std::cout << "fegetround() = " << round_name(fegetround()) << std::endl;
131 std::cout << std::endl;
132 }
133
134 TEST_CASE("double.rounds_to_nearest") {
135 //
136 // If this function fails, we may be left in a non-standard rounding state.
137 //
138 static double volatile fmin = std::numeric_limits<double>::min();
139 fesetround(FE_UPWARD);
140 std::cout << "FE_UPWARD: fmin + 1.0 = " << fHexAndDec(fmin + 1.0)
141 << " 1.0 - fmin = " << fHexAndDec(1.0 - fmin) << std::endl;
142 CHECK(fegetround() == FE_UPWARD);
143 CHECK(fast_float::detail::rounds_to_nearest() == false);
144
145 fesetround(FE_DOWNWARD);
146 std::cout << "FE_DOWNWARD: fmin + 1.0 = " << fHexAndDec(fmin + 1.0)
147 << " 1.0 - fmin = " << fHexAndDec(1.0 - fmin) << std::endl;
148 CHECK(fegetround() == FE_DOWNWARD);
149 CHECK(fast_float::detail::rounds_to_nearest() == false);
150
151 fesetround(FE_TOWARDZERO);
152 std::cout << "FE_TOWARDZERO: fmin + 1.0 = " << fHexAndDec(fmin + 1.0)
153 << " 1.0 - fmin = " << fHexAndDec(1.0 - fmin) << std::endl;
154 CHECK(fegetround() == FE_TOWARDZERO);
155 CHECK(fast_float::detail::rounds_to_nearest() == false);
156
157 fesetround(FE_TONEAREST);
158 std::cout << "FE_TONEAREST: fmin + 1.0 = " << fHexAndDec(fmin + 1.0)
159 << " 1.0 - fmin = " << fHexAndDec(1.0 - fmin) << std::endl;
160 CHECK(fegetround() == FE_TONEAREST);
161 #if (FLT_EVAL_METHOD == 1) || (FLT_EVAL_METHOD == 0)
162 CHECK(fast_float::detail::rounds_to_nearest() == true);
163 #endif
164 }
165
166 TEST_CASE("double.parse_zero") {
167 //
168 // If this function fails, we may be left in a non-standard rounding state.
169 //
170 char const *zero = "0";
171 uint64_t float64_parsed;
172 double f = 0;
173 ::memcpy(&float64_parsed, &f, sizeof(f));
174 CHECK(float64_parsed == 0);
175
176 fesetround(FE_UPWARD);
177 auto r1 = fast_float::from_chars(zero, zero + 1, f);
178 CHECK(r1.ec == std::errc());
179 std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl;
180 CHECK(f == 0.);
181 ::memcpy(&float64_parsed, &f, sizeof(f));
182 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
183 << std::endl;
184 CHECK(float64_parsed == 0);
185
186 fesetround(FE_TOWARDZERO);
187 auto r2 = fast_float::from_chars(zero, zero + 1, f);
188 CHECK(r2.ec == std::errc());
189 std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl;
190 CHECK(f == 0.);
191 ::memcpy(&float64_parsed, &f, sizeof(f));
192 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
193 << std::endl;
194 CHECK(float64_parsed == 0);
195
196 fesetround(FE_DOWNWARD);
197 auto r3 = fast_float::from_chars(zero, zero + 1, f);
198 CHECK(r3.ec == std::errc());
199 std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl;
200 CHECK(f == 0.);
201 ::memcpy(&float64_parsed, &f, sizeof(f));
202 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
203 << std::endl;
204 CHECK(float64_parsed == 0);
205
206 fesetround(FE_TONEAREST);
207 auto r4 = fast_float::from_chars(zero, zero + 1, f);
208 CHECK(r4.ec == std::errc());
209 std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl;
210 CHECK(f == 0.);
211 ::memcpy(&float64_parsed, &f, sizeof(f));
212 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
213 << std::endl;
214 CHECK(float64_parsed == 0);
215 }
216
217 TEST_CASE("double.parse_negative_zero") {
218 //
219 // If this function fails, we may be left in a non-standard rounding state.
220 //
221 char const *negative_zero = "-0";
222 uint64_t float64_parsed;
223 double f = -0.;
224 ::memcpy(&float64_parsed, &f, sizeof(f));
225 CHECK(float64_parsed == 0x8000'0000'0000'0000);
226
227 fesetround(FE_UPWARD);
228 auto r1 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
229 CHECK(r1.ec == std::errc());
230 std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f)
231 << std::endl;
232 CHECK(f == 0.);
233 ::memcpy(&float64_parsed, &f, sizeof(f));
234 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
235 << std::endl;
236 CHECK(float64_parsed == 0x8000'0000'0000'0000);
237
238 fesetround(FE_TOWARDZERO);
239 auto r2 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
240 CHECK(r2.ec == std::errc());
241 std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f)
242 << std::endl;
243 CHECK(f == 0.);
244 ::memcpy(&float64_parsed, &f, sizeof(f));
245 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
246 << std::endl;
247 CHECK(float64_parsed == 0x8000'0000'0000'0000);
248
249 fesetround(FE_DOWNWARD);
250 auto r3 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
251 CHECK(r3.ec == std::errc());
252 std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f)
253 << std::endl;
254 CHECK(f == 0.);
255 ::memcpy(&float64_parsed, &f, sizeof(f));
256 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
257 << std::endl;
258 CHECK(float64_parsed == 0x8000'0000'0000'0000);
259
260 fesetround(FE_TONEAREST);
261 auto r4 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
262 CHECK(r4.ec == std::errc());
263 std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f)
264 << std::endl;
265 CHECK(f == 0.);
266 ::memcpy(&float64_parsed, &f, sizeof(f));
267 std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
268 << std::endl;
269 CHECK(float64_parsed == 0x8000'0000'0000'0000);
270 }
271
272 TEST_CASE("float.rounds_to_nearest") {
273 //
274 // If this function fails, we may be left in a non-standard rounding state.
275 //
276 static float volatile fmin = std::numeric_limits<float>::min();
277 fesetround(FE_UPWARD);
278 std::cout << "FE_UPWARD: fmin + 1.0f = " << fHexAndDec(fmin + 1.0f)
279 << " 1.0f - fmin = " << fHexAndDec(1.0f - fmin) << std::endl;
280 CHECK(fegetround() == FE_UPWARD);
281 CHECK(fast_float::detail::rounds_to_nearest() == false);
282
283 fesetround(FE_DOWNWARD);
284 std::cout << "FE_DOWNWARD: fmin + 1.0f = " << fHexAndDec(fmin + 1.0f)
285 << " 1.0f - fmin = " << fHexAndDec(1.0f - fmin) << std::endl;
286 CHECK(fegetround() == FE_DOWNWARD);
287 CHECK(fast_float::detail::rounds_to_nearest() == false);
288
289 fesetround(FE_TOWARDZERO);
290 std::cout << "FE_TOWARDZERO: fmin + 1.0f = " << fHexAndDec(fmin + 1.0f)
291 << " 1.0f - fmin = " << fHexAndDec(1.0f - fmin) << std::endl;
292 CHECK(fegetround() == FE_TOWARDZERO);
293 CHECK(fast_float::detail::rounds_to_nearest() == false);
294
295 fesetround(FE_TONEAREST);
296 std::cout << "FE_TONEAREST: fmin + 1.0f = " << fHexAndDec(fmin + 1.0f)
297 << " 1.0f - fmin = " << fHexAndDec(1.0f - fmin) << std::endl;
298 CHECK(fegetround() == FE_TONEAREST);
299 #if (FLT_EVAL_METHOD == 1) || (FLT_EVAL_METHOD == 0)
300 CHECK(fast_float::detail::rounds_to_nearest() == true);
301 #endif
302 }
303
304 TEST_CASE("float.parse_zero") {
305 //
306 // If this function fails, we may be left in a non-standard rounding state.
307 //
308 char const *zero = "0";
309 uint32_t float32_parsed;
310 float f = 0;
311 ::memcpy(&float32_parsed, &f, sizeof(f));
312 CHECK(float32_parsed == 0);
313
314 fesetround(FE_UPWARD);
315 auto r1 = fast_float::from_chars(zero, zero + 1, f);
316 CHECK(r1.ec == std::errc());
317 std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl;
318 CHECK(f == 0.f);
319 ::memcpy(&float32_parsed, &f, sizeof(f));
320 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
321 << std::endl;
322 CHECK(float32_parsed == 0);
323
324 fesetround(FE_TOWARDZERO);
325 auto r2 = fast_float::from_chars(zero, zero + 1, f);
326 CHECK(r2.ec == std::errc());
327 std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl;
328 CHECK(f == 0.f);
329 ::memcpy(&float32_parsed, &f, sizeof(f));
330 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
331 << std::endl;
332 CHECK(float32_parsed == 0);
333
334 fesetround(FE_DOWNWARD);
335 auto r3 = fast_float::from_chars(zero, zero + 1, f);
336 CHECK(r3.ec == std::errc());
337 std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl;
338 CHECK(f == 0.f);
339 ::memcpy(&float32_parsed, &f, sizeof(f));
340 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
341 << std::endl;
342 CHECK(float32_parsed == 0);
343
344 fesetround(FE_TONEAREST);
345 auto r4 = fast_float::from_chars(zero, zero + 1, f);
346 CHECK(r4.ec == std::errc());
347 std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl;
348 CHECK(f == 0.f);
349 ::memcpy(&float32_parsed, &f, sizeof(f));
350 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
351 << std::endl;
352 CHECK(float32_parsed == 0);
353 }
354
355 TEST_CASE("float.parse_negative_zero") {
356 //
357 // If this function fails, we may be left in a non-standard rounding state.
358 //
359 char const *negative_zero = "-0";
360 uint32_t float32_parsed;
361 float f = -0.;
362 ::memcpy(&float32_parsed, &f, sizeof(f));
363 CHECK(float32_parsed == 0x8000'0000);
364
365 fesetround(FE_UPWARD);
366 auto r1 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
367 CHECK(r1.ec == std::errc());
368 std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f)
369 << std::endl;
370 CHECK(f == 0.f);
371 ::memcpy(&float32_parsed, &f, sizeof(f));
372 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
373 << std::endl;
374 CHECK(float32_parsed == 0x8000'0000);
375
376 fesetround(FE_TOWARDZERO);
377 auto r2 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
378 CHECK(r2.ec == std::errc());
379 std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f)
380 << std::endl;
381 CHECK(f == 0.f);
382 ::memcpy(&float32_parsed, &f, sizeof(f));
383 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
384 << std::endl;
385 CHECK(float32_parsed == 0x8000'0000);
386
387 fesetround(FE_DOWNWARD);
388 auto r3 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
389 CHECK(r3.ec == std::errc());
390 std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f)
391 << std::endl;
392 CHECK(f == 0.f);
393 ::memcpy(&float32_parsed, &f, sizeof(f));
394 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
395 << std::endl;
396 CHECK(float32_parsed == 0x8000'0000);
397
398 fesetround(FE_TONEAREST);
399 auto r4 = fast_float::from_chars(negative_zero, negative_zero + 2, f);
400 CHECK(r4.ec == std::errc());
401 std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f)
402 << std::endl;
403 CHECK(f == 0.f);
404 ::memcpy(&float32_parsed, &f, sizeof(f));
405 std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
406 << std::endl;
407 CHECK(float32_parsed == 0x8000'0000);
408 }
409
410 #if FASTFLOAT_SUPPLEMENTAL_TESTS
411 // C++ 17 because it is otherwise annoying to browse all files in a directory.
412 // We also only run these tests on little endian systems.
413 #if (FASTFLOAT_CPLUSPLUS >= 201703L) && (FASTFLOAT_IS_BIG_ENDIAN == 0) && \
414 !defined(FASTFLOAT_ODDPLATFORM)
415
416 #include <filesystem>
417 #include <charconv>
418
419 // return true on success
420 bool check_file(std::string file_name) {
421 std::cout << "Checking " << file_name << std::endl;
422 // We check all rounding directions, for each file.
423 std::vector<int> directions = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
424 FE_TONEAREST};
425 for (int d : directions) {
426 std::cout << "fesetround to " << round_name(d) << std::endl;
427 fesetround(d);
428 size_t number{0};
429 std::fstream newfile(file_name, std::ios::in);
430 if (newfile.is_open()) {
431 std::string str;
432 while (std::getline(newfile, str)) {
433 if (str.size() > 0) {
434 #ifdef __STDCPP_FLOAT16_T__
435 // Read 16-bit hex
436 uint16_t float16{};
437 auto r16 =
438 std::from_chars(str.data(), str.data() + str.size(), float16, 16);
439 if (r16.ec != std::errc()) {
440 std::cerr << "16-bit parsing failure: " << str << "\n";
441 return false;
442 }
443 #endif
444 // Read 32-bit hex
445 uint32_t float32{};
446 auto r32 = std::from_chars(str.data() + 5, str.data() + str.size(),
447 float32, 16);
448 if (r32.ec != std::errc()) {
449 std::cerr << "32-bit parsing failure: " << str << "\n";
450 return false;
451 }
452 // Read 64-bit hex
453 uint64_t float64{};
454 auto r64 = std::from_chars(str.data() + 14, str.data() + str.size(),
455 float64, 16);
456 if (r64.ec != std::errc()) {
457 std::cerr << "64-bit parsing failure: " << str << "\n";
458 return false;
459 }
460 // The string to parse:
461 char const *number_string = str.data() + 31;
462 char const *end_of_string = str.data() + str.size();
463 #ifdef __STDCPP_FLOAT16_T__
464 // Parse as 16-bit float
465 std::float16_t parsed_16{};
466 auto fast_float_r16 =
467 fast_float::from_chars(number_string, end_of_string, parsed_16);
468 if (fast_float_r16.ec != std::errc() &&
469 fast_float_r16.ec != std::errc::result_out_of_range) {
470 std::cerr << "16-bit fast_float parsing failure: " << str << "\n";
471 return false;
472 }
473 #endif
474 // Parse as 32-bit float
475 float parsed_32{};
476 auto fast_float_r32 =
477 fast_float::from_chars(number_string, end_of_string, parsed_32);
478 if (fast_float_r32.ec != std::errc() &&
479 fast_float_r32.ec != std::errc::result_out_of_range) {
480 std::cerr << "32-bit fast_float parsing failure: " << str << "\n";
481 return false;
482 }
483 // Parse as 64-bit float
484 double parsed_64{};
485 auto fast_float_r64 =
486 fast_float::from_chars(number_string, end_of_string, parsed_64);
487 if (fast_float_r64.ec != std::errc() &&
488 fast_float_r32.ec != std::errc::result_out_of_range) {
489 std::cerr << "64-bit fast_float parsing failure: " << str << "\n";
490 return false;
491 }
492 // Convert the floats to unsigned ints.
493 #ifdef __STDCPP_FLOAT16_T__
494 uint16_t float16_parsed{};
495 #endif
496 uint32_t float32_parsed{};
497 uint64_t float64_parsed{};
498 #ifdef __STDCPP_FLOAT16_T__
499 ::memcpy(&float16_parsed, &parsed_16, sizeof(parsed_16));
500
501 #endif
502 ::memcpy(&float32_parsed, &parsed_32, sizeof(parsed_32));
503 ::memcpy(&float64_parsed, &parsed_64, sizeof(parsed_64));
504 // Compare with expected results
505 #ifdef __STDCPP_FLOAT16_T__
506 if (float16_parsed != float16) {
507 std::cout << "bad 16: " << str << std::endl;
508 std::cout << "parsed as " << fHexAndDec(parsed_16) << std::endl;
509 std::cout << "as raw uint16_t, parsed = " << float16_parsed
510 << ", expected = " << float16 << std::endl;
511 std::cout << "fesetround: " << round_name(d) << std::endl;
512 const bool is_ulfjack =
513 file_name.find("ulfjack") != std::string::npos;
514 if (is_ulfjack) {
515 std::cout << "This is a known issue with ulfjack's test suite."
516 << std::endl;
517 } else {
518 fesetround(FE_TONEAREST);
519 return false;
520 }
521 }
522 #endif
523 if (float32_parsed != float32) {
524 std::cout << "bad 32: " << str << std::endl;
525 std::cout << "parsed as " << fHexAndDec(parsed_32) << std::endl;
526 std::cout << "as raw uint32_t, parsed = " << float32_parsed
527 << ", expected = " << float32 << std::endl;
528 std::cout << "fesetround: " << round_name(d) << std::endl;
529 fesetround(FE_TONEAREST);
530 return false;
531 }
532 if (float64_parsed != float64) {
533 std::cout << "bad 64: " << str << std::endl;
534 std::cout << "parsed as " << fHexAndDec(parsed_64) << std::endl;
535 std::cout << "as raw uint64_t, parsed = " << float64_parsed
536 << ", expected = " << float64 << std::endl;
537 std::cout << "fesetround: " << round_name(d) << std::endl;
538 fesetround(FE_TONEAREST);
539 return false;
540 }
541 number++;
542 }
543 }
544 std::cout << "checked " << std::defaultfloat << number << " values"
545 << std::endl;
546 newfile.close(); // close the file object
547 } else {
548 std::cout << "Could not read " << file_name << std::endl;
549 fesetround(FE_TONEAREST);
550 return false;
551 }
552 }
553 fesetround(FE_TONEAREST);
554 return true;
555 }
556
557 TEST_CASE("supplemental") {
558 std::string path = SUPPLEMENTAL_TEST_DATA_DIR;
559 for (auto const &entry : std::filesystem::directory_iterator(path)) {
560 const auto file = entry.path().string();
561 CAPTURE(file);
562 CHECK(check_file(file));
563 }
564 }
565 #endif
566 #endif
567
568 TEST_CASE("leading_zeroes") {
569 constexpr uint64_t const bit = 1;
570 CHECK(fast_float::leading_zeroes(bit << 0) == 63);
571 CHECK(fast_float::leading_zeroes(bit << 1) == 62);
572 CHECK(fast_float::leading_zeroes(bit << 2) == 61);
573 CHECK(fast_float::leading_zeroes(bit << 61) == 2);
574 CHECK(fast_float::leading_zeroes(bit << 62) == 1);
575 CHECK(fast_float::leading_zeroes(bit << 63) == 0);
576 }
577
578 void test_full_multiplication(uint64_t lhs, uint64_t rhs, uint64_t expected_lo,
579 uint64_t expected_hi) {
580 fast_float::value128 v;
581 v = fast_float::full_multiplication(lhs, rhs);
582 INFO("lhs=" << iHexAndDec(lhs) << " "
583 << "rhs=" << iHexAndDec(rhs)
584 << "\n actualLo=" << iHexAndDec(v.low) << " "
585 << "actualHi=" << iHexAndDec(v.high)
586 << "\n expectedLo=" << iHexAndDec(expected_lo) << " "
587 << "expectedHi=" << iHexAndDec(expected_hi));
588 CHECK_EQ(v.low, expected_lo);
589 CHECK_EQ(v.high, expected_hi);
590 v = fast_float::full_multiplication(rhs, lhs);
591 CHECK_EQ(v.low, expected_lo);
592 CHECK_EQ(v.high, expected_hi);
593 }
594
595 TEST_CASE("full_multiplication") {
596 constexpr uint64_t const bit = 1;
597 // lhs rhs lo hi
598 test_full_multiplication(bit << 0, bit << 0, 1u, 0u);
599 test_full_multiplication(bit << 0, bit << 63, bit << 63, 0u);
600 test_full_multiplication(bit << 1, bit << 63, 0u, 1u);
601 test_full_multiplication(bit << 63, bit << 0, bit << 63, 0u);
602 test_full_multiplication(bit << 63, bit << 1, 0u, 1u);
603 test_full_multiplication(bit << 63, bit << 2, 0u, 2u);
604 test_full_multiplication(bit << 63, bit << 63, 0u, bit << 62);
605 }
606
607 TEST_CASE("issue8") {
608 char const *s =
609 "3."
610 "141592653589793238462643383279502884197169399375105820974944592307816406"
611 "286208998628034825342117067982148086513282306647093844609550582231725359"
612 "408128481117450284102701938521105559644622948954930381964428810975665933"
613 "446128475648233786783165271201909145648566923460348610454326648213393607"
614 "260249141273724587006606315588174881520920962829254091715364367892590360"
615 "011330530548820466521384146951941511609433057270365759591953092186117381"
616 "932611793105118548074462379962749567351885752724891227938183011949129833"
617 "673362440656643086021394946395224737190702179860943702770539217176293176"
618 "752384674818467669405132000568127145263560827785771342757789609173637178"
619 "721468440901224953430146549585371050792279689258923542019956112129021960"
620 "864034418159813629774771309960518707211349999998372978";
621 for (int i = 0; i < 16; i++) {
622 // Parse all but the last i chars. We should still get 3.141ish.
623 double d = 0.0;
624 auto answer = fast_float::from_chars(s, s + strlen(s) - i, d);
625 CHECK_MESSAGE(answer.ec == std::errc(), "i=" << i);
626 CHECK_MESSAGE(d == 0x1.921fb54442d18p+1,
627 "i=" << i << "\n"
628 << std::string(s, strlen(s) - size_t(i)) << "\n"
629 << std::hexfloat << d << "\n"
630 << std::defaultfloat << "\n");
631 }
632 }
633
634 TEST_CASE("check_behavior") {
635 std::string const input = "abc";
636 double result;
637 auto answer =
638 fast_float::from_chars(input.data(), input.data() + input.size(), result);
639 CHECK_MESSAGE(answer.ec != std::errc(), "expected parse failure");
640 CHECK_MESSAGE(
641 answer.ptr == input.data(),
642 "If there is no pattern match, we should have ptr equals first");
643 }
644
645 TEST_CASE("decimal_point_parsing") {
646 double result;
647 fast_float::parse_options options{};
648 {
649 std::string const input = "1,25";
650 auto answer = fast_float::from_chars_advanced(
651 input.data(), input.data() + input.size(), result, options);
652 CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success");
653 CHECK_MESSAGE(answer.ptr == input.data() + 1,
654 "Parsing should have stopped at comma");
655 CHECK_EQ(result, 1.0);
656
657 options.decimal_point = ',';
658 answer = fast_float::from_chars_advanced(
659 input.data(), input.data() + input.size(), result, options);
660 CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success");
661 CHECK_MESSAGE(answer.ptr == input.data() + input.size(),
662 "Parsing should have stopped at end");
663 CHECK_EQ(result, 1.25);
664 }
665 {
666 std::string const input = "1.25";
667 auto answer = fast_float::from_chars_advanced(
668 input.data(), input.data() + input.size(), result, options);
669 CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success");
670 CHECK_MESSAGE(answer.ptr == input.data() + 1,
671 "Parsing should have stopped at dot");
672 CHECK_EQ(result, 1.0);
673
674 options.decimal_point = '.';
675 answer = fast_float::from_chars_advanced(
676 input.data(), input.data() + input.size(), result, options);
677 CHECK_MESSAGE(answer.ec == std::errc(), "expected parse success");
678 CHECK_MESSAGE(answer.ptr == input.data() + input.size(),
679 "Parsing should have stopped at end");
680 CHECK_EQ(result, 1.25);
681 }
682 }
683
684 TEST_CASE("issue19") {
685 std::string const input = "234532.3426362,7869234.9823,324562.645";
686 double result;
687 auto answer =
688 fast_float::from_chars(input.data(), input.data() + input.size(), result);
689 CHECK_MESSAGE(answer.ec == std::errc(),
690 "We want to parse up to 234532.3426362\n");
691 CHECK_MESSAGE(answer.ptr == input.data() + 14,
692 "Parsed the number "
693 << result << " and stopped at the wrong character: after "
694 << (answer.ptr - input.data()) << " characters");
695 CHECK_MESSAGE(result == 234532.3426362, "We want to parse234532.3426362\n");
696 CHECK_MESSAGE(answer.ptr[0] == ',', "We want to parse up to the comma\n");
697
698 answer = fast_float::from_chars(answer.ptr + 1, input.data() + input.size(),
699 result);
700 CHECK_MESSAGE(answer.ec == std::errc(), "We want to parse 7869234.9823\n");
701 CHECK_MESSAGE(answer.ptr == input.data() + 27,
702 "Parsed the number " << result
703 << " and stopped at the wrong character "
704 << (answer.ptr - input.data()));
705 CHECK_MESSAGE(answer.ptr[0] == ',', "We want to parse up to the comma\n");
706 CHECK_MESSAGE(result == 7869234.9823, "We want to parse up 7869234.9823\n");
707
708 answer = fast_float::from_chars(answer.ptr + 1, input.data() + input.size(),
709 result);
710 CHECK_MESSAGE(answer.ec == std::errc(), "We want to parse 324562.645\n");
711 CHECK_MESSAGE(answer.ptr == input.data() + 38,
712 "Parsed the number " << result
713 << " and stopped at the wrong character "
714 << (answer.ptr - input.data()));
715 CHECK_MESSAGE(result == 324562.645, "We want to parse up 7869234.9823\n");
716 }
717
718 TEST_CASE("issue19") {
719 std::string const input = "3.14e";
720 double result;
721 auto answer =
722 fast_float::from_chars(input.data(), input.data() + input.size(), result);
723 CHECK_MESSAGE(answer.ec == std::errc(), "We want to parse up to 3.14\n");
724 CHECK_MESSAGE(answer.ptr == input.data() + 4,
725 "Parsed the number "
726 << result << " and stopped at the wrong character: after "
727 << (answer.ptr - input.data()) << " characters");
728 }
729
730 TEST_CASE("scientific_only") {
731 // first, we try with something that should fail...
732 std::string input = "3.14";
733 double result;
734 auto answer =
735 fast_float::from_chars(input.data(), input.data() + input.size(), result,
736 fast_float::chars_format::scientific);
737 CHECK_MESSAGE(answer.ec != std::errc(),
738 "It is not scientific! Parsed: " << result);
739
740 input = "3.14e10";
741 answer = fast_float::from_chars(input.data(), input.data() + input.size(),
742 result, fast_float::chars_format::scientific);
743 CHECK_MESSAGE(answer.ec == std::errc(),
744 "It is scientific! Parsed: " << result);
745 CHECK_MESSAGE(answer.ptr == input.data() + input.size(),
746 "Parsed the number "
747 << result << " and stopped at the wrong character: after "
748 << (answer.ptr - input.data()) << " characters");
749 }
750
751 TEST_CASE("test_fixed_only") {
752 std::string const input = "3.14e10";
753 double result;
754 auto answer =
755 fast_float::from_chars(input.data(), input.data() + input.size(), result,
756 fast_float::chars_format::fixed);
757 CHECK_MESSAGE(answer.ec == std::errc(),
758 "We want to parse up to 3.14; parsed: " << result);
759 CHECK_MESSAGE(answer.ptr == input.data() + 4,
760 "Parsed the number "
761 << result << " and stopped at the wrong character: after "
762 << (answer.ptr - input.data()) << " characters");
763 }
764
765 static double const testing_power_of_ten[] = {
766 1e-323, 1e-322, 1e-321, 1e-320, 1e-319, 1e-318, 1e-317, 1e-316, 1e-315,
767 1e-314, 1e-313, 1e-312, 1e-311, 1e-310, 1e-309, 1e-308,
768
769 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301, 1e-300, 1e-299,
770 1e-298, 1e-297, 1e-296, 1e-295, 1e-294, 1e-293, 1e-292, 1e-291, 1e-290,
771 1e-289, 1e-288, 1e-287, 1e-286, 1e-285, 1e-284, 1e-283, 1e-282, 1e-281,
772 1e-280, 1e-279, 1e-278, 1e-277, 1e-276, 1e-275, 1e-274, 1e-273, 1e-272,
773 1e-271, 1e-270, 1e-269, 1e-268, 1e-267, 1e-266, 1e-265, 1e-264, 1e-263,
774 1e-262, 1e-261, 1e-260, 1e-259, 1e-258, 1e-257, 1e-256, 1e-255, 1e-254,
775 1e-253, 1e-252, 1e-251, 1e-250, 1e-249, 1e-248, 1e-247, 1e-246, 1e-245,
776 1e-244, 1e-243, 1e-242, 1e-241, 1e-240, 1e-239, 1e-238, 1e-237, 1e-236,
777 1e-235, 1e-234, 1e-233, 1e-232, 1e-231, 1e-230, 1e-229, 1e-228, 1e-227,
778 1e-226, 1e-225, 1e-224, 1e-223, 1e-222, 1e-221, 1e-220, 1e-219, 1e-218,
779 1e-217, 1e-216, 1e-215, 1e-214, 1e-213, 1e-212, 1e-211, 1e-210, 1e-209,
780 1e-208, 1e-207, 1e-206, 1e-205, 1e-204, 1e-203, 1e-202, 1e-201, 1e-200,
781 1e-199, 1e-198, 1e-197, 1e-196, 1e-195, 1e-194, 1e-193, 1e-192, 1e-191,
782 1e-190, 1e-189, 1e-188, 1e-187, 1e-186, 1e-185, 1e-184, 1e-183, 1e-182,
783 1e-181, 1e-180, 1e-179, 1e-178, 1e-177, 1e-176, 1e-175, 1e-174, 1e-173,
784 1e-172, 1e-171, 1e-170, 1e-169, 1e-168, 1e-167, 1e-166, 1e-165, 1e-164,
785 1e-163, 1e-162, 1e-161, 1e-160, 1e-159, 1e-158, 1e-157, 1e-156, 1e-155,
786 1e-154, 1e-153, 1e-152, 1e-151, 1e-150, 1e-149, 1e-148, 1e-147, 1e-146,
787 1e-145, 1e-144, 1e-143, 1e-142, 1e-141, 1e-140, 1e-139, 1e-138, 1e-137,
788 1e-136, 1e-135, 1e-134, 1e-133, 1e-132, 1e-131, 1e-130, 1e-129, 1e-128,
789 1e-127, 1e-126, 1e-125, 1e-124, 1e-123, 1e-122, 1e-121, 1e-120, 1e-119,
790 1e-118, 1e-117, 1e-116, 1e-115, 1e-114, 1e-113, 1e-112, 1e-111, 1e-110,
791 1e-109, 1e-108, 1e-107, 1e-106, 1e-105, 1e-104, 1e-103, 1e-102, 1e-101,
792 1e-100, 1e-99, 1e-98, 1e-97, 1e-96, 1e-95, 1e-94, 1e-93, 1e-92,
793 1e-91, 1e-90, 1e-89, 1e-88, 1e-87, 1e-86, 1e-85, 1e-84, 1e-83,
794 1e-82, 1e-81, 1e-80, 1e-79, 1e-78, 1e-77, 1e-76, 1e-75, 1e-74,
795 1e-73, 1e-72, 1e-71, 1e-70, 1e-69, 1e-68, 1e-67, 1e-66, 1e-65,
796 1e-64, 1e-63, 1e-62, 1e-61, 1e-60, 1e-59, 1e-58, 1e-57, 1e-56,
797 1e-55, 1e-54, 1e-53, 1e-52, 1e-51, 1e-50, 1e-49, 1e-48, 1e-47,
798 1e-46, 1e-45, 1e-44, 1e-43, 1e-42, 1e-41, 1e-40, 1e-39, 1e-38,
799 1e-37, 1e-36, 1e-35, 1e-34, 1e-33, 1e-32, 1e-31, 1e-30, 1e-29,
800 1e-28, 1e-27, 1e-26, 1e-25, 1e-24, 1e-23, 1e-22, 1e-21, 1e-20,
801 1e-19, 1e-18, 1e-17, 1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11,
802 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2,
803 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
804 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16,
805 1e17, 1e18, 1e19, 1e20, 1e21, 1e22, 1e23, 1e24, 1e25,
806 1e26, 1e27, 1e28, 1e29, 1e30, 1e31, 1e32, 1e33, 1e34,
807 1e35, 1e36, 1e37, 1e38, 1e39, 1e40, 1e41, 1e42, 1e43,
808 1e44, 1e45, 1e46, 1e47, 1e48, 1e49, 1e50, 1e51, 1e52,
809 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59, 1e60, 1e61,
810 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69, 1e70,
811 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
812 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88,
813 1e89, 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97,
814 1e98, 1e99, 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106,
815 1e107, 1e108, 1e109, 1e110, 1e111, 1e112, 1e113, 1e114, 1e115,
816 1e116, 1e117, 1e118, 1e119, 1e120, 1e121, 1e122, 1e123, 1e124,
817 1e125, 1e126, 1e127, 1e128, 1e129, 1e130, 1e131, 1e132, 1e133,
818 1e134, 1e135, 1e136, 1e137, 1e138, 1e139, 1e140, 1e141, 1e142,
819 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149, 1e150, 1e151,
820 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159, 1e160,
821 1e161, 1e162, 1e163, 1e164, 1e165, 1e166, 1e167, 1e168, 1e169,
822 1e170, 1e171, 1e172, 1e173, 1e174, 1e175, 1e176, 1e177, 1e178,
823 1e179, 1e180, 1e181, 1e182, 1e183, 1e184, 1e185, 1e186, 1e187,
824 1e188, 1e189, 1e190, 1e191, 1e192, 1e193, 1e194, 1e195, 1e196,
825 1e197, 1e198, 1e199, 1e200, 1e201, 1e202, 1e203, 1e204, 1e205,
826 1e206, 1e207, 1e208, 1e209, 1e210, 1e211, 1e212, 1e213, 1e214,
827 1e215, 1e216, 1e217, 1e218, 1e219, 1e220, 1e221, 1e222, 1e223,
828 1e224, 1e225, 1e226, 1e227, 1e228, 1e229, 1e230, 1e231, 1e232,
829 1e233, 1e234, 1e235, 1e236, 1e237, 1e238, 1e239, 1e240, 1e241,
830 1e242, 1e243, 1e244, 1e245, 1e246, 1e247, 1e248, 1e249, 1e250,
831 1e251, 1e252, 1e253, 1e254, 1e255, 1e256, 1e257, 1e258, 1e259,
832 1e260, 1e261, 1e262, 1e263, 1e264, 1e265, 1e266, 1e267, 1e268,
833 1e269, 1e270, 1e271, 1e272, 1e273, 1e274, 1e275, 1e276, 1e277,
834 1e278, 1e279, 1e280, 1e281, 1e282, 1e283, 1e284, 1e285, 1e286,
835 1e287, 1e288, 1e289, 1e290, 1e291, 1e292, 1e293, 1e294, 1e295,
836 1e296, 1e297, 1e298, 1e299, 1e300, 1e301, 1e302, 1e303, 1e304,
837 1e305, 1e306, 1e307, 1e308};
838
839 TEST_CASE("powers_of_ten") {
840 char buf[1024];
841 WARN_MESSAGE(1e-308 == std::pow(10, -308),
842 "On your system, the pow function is busted. Sorry about that.");
843 bool is_pow_correct{1e-308 == std::pow(10, -308)};
844 // large negative values should be zero.
845 int start_point = is_pow_correct ? -1000 : -307;
846 for (int i = start_point; i <= 308; ++i) {
847 INFO("i=" << i);
848 size_t n = size_t(snprintf(buf, sizeof(buf), "1e%d", i));
849 REQUIRE(n < sizeof(buf)); // if false, fails the test and exits
850 double actual;
851 auto result = fast_float::from_chars(buf, buf + 1000, actual);
852 double expected =
853 ((i >= -323) ? testing_power_of_ten[i + 323] : std::pow(10, i));
854 auto expected_ec =
855 (i < -323 || i > 308) ? std::errc::result_out_of_range : std::errc();
856 CHECK_MESSAGE(result.ec == expected_ec, " I could not parse " << buf);
857 CHECK_MESSAGE(actual == expected,
858 "String '" << buf << "'parsed to " << actual);
859 }
860 }
861
862 template <typename T> std::string to_string(T d) {
863 std::string s(64, '\0');
864 auto written = std::snprintf(&s[0], s.size(), "%.*e",
865 std::numeric_limits<T>::max_digits10 - 1, d);
866 s.resize(size_t(written));
867 return s;
868 }
869
870 template <typename T> std::string to_long_string(T d) {
871 std::string s(4096, '\0');
872 auto written = std::snprintf(&s[0], s.size(), "%.*e",
873 std::numeric_limits<T>::max_digits10 * 10, d);
874 s.resize(size_t(written));
875 return s;
876 }
877
878 uint32_t get_mantissa(float f) {
879 uint32_t m;
880 memcpy(&m, &f, sizeof(f));
881 return (m & ((uint32_t(1) << 23) - 1));
882 }
883
884 uint64_t get_mantissa(double f) {
885 uint64_t m;
886 memcpy(&m, &f, sizeof(f));
887 return (m & ((uint64_t(1) << 57) - 1));
888 }
889
890 #ifdef __STDCPP_FLOAT64_T__
891 uint64_t get_mantissa(std::float64_t f) {
892 uint64_t m;
893 memcpy(&m, &f, sizeof(f));
894 return (m & ((uint64_t(1) << 10) - 1));
895 }
896 #endif
897
898 #ifdef __STDCPP_FLOAT32_T__
899 uint32_t get_mantissa(std::float32_t f) {
900 uint32_t m;
901 memcpy(&m, &f, sizeof(f));
902 return (m & ((uint32_t(1) << 10) - 1));
903 }
904 #endif
905
906 #ifdef __STDCPP_FLOAT16_T__
907 uint16_t get_mantissa(std::float16_t f) {
908 uint16_t m;
909 memcpy(&m, &f, sizeof(f));
910 return (m & ((uint16_t(1) << 10) - 1));
911 }
912 #endif
913
914 #ifdef __STDCPP_BFLOAT16_T__
915 uint16_t get_mantissa(std::bfloat16_t f) {
916 uint16_t m;
917 memcpy(&m, &f, sizeof(f));
918 return (m & ((uint16_t(1) << 7) - 1));
919 }
920 #endif
921
922 std::string append_zeros(std::string str, size_t number_of_zeros) {
923 std::string answer(str);
924 for (size_t i = 0; i < number_of_zeros; i++) {
925 answer += "0";
926 }
927 return answer;
928 }
929
930 namespace {
931
932 enum class Diag { runtime, comptime };
933
934 } // anonymous namespace
935
936 constexpr size_t global_string_capacity = 2048;
937
938 template <Diag diag, class T, typename result_type, typename stringtype>
939 constexpr void check_basic_test_result(stringtype str, result_type result,
940 T actual, T expected,
941 std::errc expected_ec) {
942 struct ComptimeDiag {
943 // Purposely not constexpr
944 static void error_not_equal() {}
945 };
946
947 #define FASTFLOAT_CHECK_EQ(...) \
948 if constexpr (diag == Diag::runtime) { \
949 char narrow[global_string_capacity]{}; \
950 for (size_t i = 0; i < str.size(); i++) { \
951 narrow[i] = char(str[i]); \
952 } \
953 INFO("str(char" << 8 * sizeof(typename stringtype::value_type) \
954 << ")=" << narrow << "\n" \
955 << " expected=" << fHexAndDec(expected) << "\n" \
956 << " ..actual=" << fHexAndDec(actual) << "\n" \
957 << " expected mantissa=" \
958 << iHexAndDec(get_mantissa(expected)) << "\n" \
959 << " ..actual mantissa=" \
960 << iHexAndDec(get_mantissa(actual))); \
961 CHECK_EQ(__VA_ARGS__); \
962 } else { \
963 if ([](auto const &lhs, auto const &rhs) { \
964 return lhs != rhs; \
965 }(__VA_ARGS__)) { \
966 ComptimeDiag::error_not_equal(); \
967 } \
968 }
969
970 auto copysign = [](double x, double y) -> double {
971 #if FASTFLOAT_HAS_BIT_CAST
972 if (fast_float::cpp20_and_in_constexpr()) {
973 using equiv_int = std::make_signed_t<fast_float::equiv_uint_t<double>>;
974 auto const i = std::bit_cast<equiv_int>(y);
975 if (i < 0) {
976 return -x;
977 }
978 return x;
979 } else
980 #endif
981 return std::copysign(x, y);
982 };
983
984 auto isnan = [](double x) -> bool { return x != x; };
985
986 FASTFLOAT_CHECK_EQ(result.ec, expected_ec);
987 FASTFLOAT_CHECK_EQ(result.ptr, str.data() + str.size());
988 FASTFLOAT_CHECK_EQ(copysign(1, actual), copysign(1, expected));
989 FASTFLOAT_CHECK_EQ(isnan(actual), isnan(expected));
990 FASTFLOAT_CHECK_EQ(actual, expected);
991
992 #undef FASTFLOAT_CHECK_EQ
993 }
994
995 template <Diag diag, class T>
996 constexpr void basic_test(std::string_view str, T expected,
997 std::errc expected_ec = std::errc()) {
998 T actual{};
999 auto result =
1000 fast_float::from_chars(str.data(), str.data() + str.size(), actual);
1001 check_basic_test_result<diag>(str, result, actual, expected, expected_ec);
1002
1003 if (str.size() > global_string_capacity) {
1004 return;
1005 }
1006
1007 // We give plenty of memory: 2048 characters.
1008 char16_t u16[global_string_capacity]{};
1009 for (size_t i = 0; i < str.size(); i++) {
1010 u16[i] = char16_t(str[i]);
1011 }
1012
1013 auto result16 = fast_float::from_chars(u16, u16 + str.size(), actual);
1014 check_basic_test_result<diag>(std::u16string_view(u16, str.size()), result16,
1015 actual, expected, expected_ec);
1016
1017 char32_t u32[global_string_capacity]{};
1018 for (size_t i = 0; i < str.size(); i++) {
1019 u32[i] = char32_t(str[i]);
1020 }
1021
1022 auto result32 = fast_float::from_chars(u32, u32 + str.size(), actual);
1023 check_basic_test_result<diag>(std::u32string_view(u32, str.size()), result32,
1024 actual, expected, expected_ec);
1025 }
1026
1027 template <Diag diag, class T>
1028 constexpr void basic_test(std::string_view str, T expected,
1029 fast_float::parse_options options) {
1030 T actual{};
1031 auto result = fast_float::from_chars_advanced(
1032 str.data(), str.data() + str.size(), actual, options);
1033 check_basic_test_result<diag>(str, result, actual, expected, std::errc());
1034 }
1035
1036 template <Diag diag, class T>
1037 constexpr void basic_test(std::string_view str, T expected,
1038 std::errc expected_ec,
1039 fast_float::parse_options options) {
1040 T actual{};
1041 auto result = fast_float::from_chars_advanced(
1042 str.data(), str.data() + str.size(), actual, options);
1043 check_basic_test_result<diag>(str, result, actual, expected, expected_ec);
1044 }
1045
1046 void basic_test(float val) {
1047 {
1048 std::string long_vals = to_long_string(val);
1049 INFO("long vals: " << long_vals);
1050 basic_test<Diag::runtime, float>(long_vals, val);
1051 }
1052 {
1053 std::string vals = to_string(val);
1054 INFO("vals: " << vals);
1055 basic_test<Diag::runtime, float>(vals, val);
1056 }
1057 }
1058
1059 #define verify_runtime(...) \
1060 do { \
1061 basic_test<Diag::runtime>(__VA_ARGS__); \
1062 } while (false)
1063
1064 #define verify_comptime(...) \
1065 do { \
1066 constexpr int verify_comptime_var = \
1067 (basic_test<Diag::comptime>(__VA_ARGS__), 0); \
1068 (void)verify_comptime_var; \
1069 } while (false)
1070
1071 #define verify_options_runtime(...) \
1072 do { \
1073 basic_test<Diag::runtime>(__VA_ARGS__, options); \
1074 } while (false)
1075
1076 #define verify_options_comptime(...) \
1077 do { \
1078 constexpr int verify_options_comptime_var = \
1079 (basic_test<Diag::comptime>(__VA_ARGS__, options), 0); \
1080 (void)verify_options_comptime_var; \
1081 } while (false)
1082
1083 #if defined(FASTFLOAT_CONSTEXPR_TESTS)
1084 #if !FASTFLOAT_IS_CONSTEXPR
1085 #error "from_chars must be constexpr for constexpr tests"
1086 #endif
1087
1088 #define verify(...) \
1089 do { \
1090 verify_runtime(__VA_ARGS__); \
1091 verify_comptime(__VA_ARGS__); \
1092 } while (false)
1093
1094 #define verify_options(...) \
1095 do { \
1096 verify_options_runtime(__VA_ARGS__); \
1097 verify_options_comptime(__VA_ARGS__); \
1098 } while (false)
1099
1100 #else
1101 #define verify verify_runtime
1102 #define verify_options verify_options_runtime
1103 #endif
1104
1105 #define verify32(val) \
1106 { \
1107 INFO(#val); \
1108 basic_test(val); \
1109 }
1110
1111 TEST_CASE("double.inf") {
1112 verify("INF", std::numeric_limits<double>::infinity());
1113 verify("-INF", -std::numeric_limits<double>::infinity());
1114 verify("INFINITY", std::numeric_limits<double>::infinity());
1115 verify("-INFINITY", -std::numeric_limits<double>::infinity());
1116 verify("infinity", std::numeric_limits<double>::infinity());
1117 verify("-infinity", -std::numeric_limits<double>::infinity());
1118 verify("inf", std::numeric_limits<double>::infinity());
1119 verify("-inf", -std::numeric_limits<double>::infinity());
1120 verify("1234456789012345678901234567890e9999999999999999999999999999",
1121 std::numeric_limits<double>::infinity(),
1122 std::errc::result_out_of_range);
1123 verify("-2139879401095466344511101915470454744.9813888656856943E+272",
1124 -std::numeric_limits<double>::infinity(),
1125 std::errc::result_out_of_range);
1126 verify("1.8e308", std::numeric_limits<double>::infinity(),
1127 std::errc::result_out_of_range);
1128 verify("1.832312213213213232132132143451234453123412321321312e308",
1129 std::numeric_limits<double>::infinity(),
1130 std::errc::result_out_of_range);
1131 verify("2e30000000000000000", std::numeric_limits<double>::infinity(),
1132 std::errc::result_out_of_range);
1133 verify("2e3000", std::numeric_limits<double>::infinity(),
1134 std::errc::result_out_of_range);
1135 verify("1.9e308", std::numeric_limits<double>::infinity(),
1136 std::errc::result_out_of_range);
1137
1138 // DBL_MAX + 0.00000000000000001e308
1139 verify("1.79769313486231581e308", std::numeric_limits<double>::infinity(),
1140 std::errc::result_out_of_range);
1141
1142 // DBL_MAX + 0.0000000000000001e308
1143 verify("1.7976931348623159e308", std::numeric_limits<double>::infinity(),
1144 std::errc::result_out_of_range);
1145 }
1146
1147 TEST_CASE("double.general") {
1148 verify("0.95000000000000000000", 0.95);
1149 verify("22250738585072012e-324",
1150 0x1p-1022); /* limit between normal and subnormal*/
1151 verify("-22250738585072012e-324",
1152 -0x1p-1022); /* limit between normal and subnormal*/
1153 verify("-1e-999", -0.0, std::errc::result_out_of_range);
1154
1155 // DBL_TRUE_MIN / 2
1156 verify("2.4703282292062327e-324", 0.0, std::errc::result_out_of_range);
1157
1158 // DBL_TRUE_MIN / 2 + 0.0000000000000001e-324
1159 verify("2.4703282292062328e-324", 0x0.0000000000001p-1022);
1160
1161 verify("-2.2222222222223e-322", -0x1.68p-1069);
1162 verify("9007199254740993.0", 0x1p+53);
1163 verify("860228122.6654514319E+90", 0x1.92bb20990715fp+328);
1164 verify_runtime(append_zeros("9007199254740993.0", 1000), 0x1p+53);
1165 verify("10000000000000000000", 0x1.158e460913dp+63);
1166 verify("10000000000000000000000000000001000000000000",
1167 0x1.cb2d6f618c879p+142);
1168 verify("10000000000000000000000000000000000000000001",
1169 0x1.cb2d6f618c879p+142);
1170 verify("1.1920928955078125e-07", 1.1920928955078125e-07);
1171 verify("9355950000000000000."
1172 "000000000000000000000000000000000018446744073709551616000001844674407"
1173 "370955161618446744073709551614073709551616184467440737095516160001844"
1174 "674407370955161660000018446744073709551616184467440737095516140737095"
1175 "516161844674407370955161600018446744073709551616018446744073709556744"
1176 "516161844674407370955161407370955161618446744073709551616000184467440"
1177 "737095516160184467440737095516116160001844674407370950018446744073709"
1178 "551616001844674407370955161600184467440737095511681644674407370955161"
1179 "600018440737095516160184467440737095516161844674407370955161600018446"
1180 "744075369107516016116160001844674407370950018446744073709551616001844"
1181 "674407370955161600184467440737095516161844674407370955161600018449551"
1182 "61618446744073709551616000184467440753691075160018446744073709",
1183 0x1.03ae05e8fca1cp+63);
1184 verify("-0", -0.0);
1185 verify(
1186 "2."
1187 "225073858507202124188701479202220329072405282794390378143031338374351073"
1188 "192441946867544064325638818513821882185024380699999477330130056498841077"
1189 "919287413419292972009704819519930679932909690427840647316820415659267286"
1190 "329336304746701233168529834221527445172608358596545663192828352447877877"
1191 "998943107797838336991592885945552137141811284582511455843192230798975043"
1192 "950868594124572308917389461693683723211913736589779777232866988403563902"
1193 "510444430354573967337065839810554204566938246584137476071559811765738776"
1194 "267476659123871999319040063173347090030127901881752034471902500280612777"
1195 "779167983910905785840064647159438105114891542827750411746821941339524666"
1196 "825034313061815878293790042053923750720833666932415800027583911188541886"
1197 "41513168478436313080237596295773983001708984375e-308",
1198 0x1.0000000000002p-1022);
1199 verify("1.0000000000000006661338147750939242541790008544921875",
1200 1.0000000000000007);
1201 verify("1090544144181609348835077142190", 0x1.b8779f2474dfbp+99);
1202 verify("2.2250738585072013e-308", 2.2250738585072013e-308);
1203 verify("-92666518056446206563E3", -92666518056446206563E3);
1204 verify("-92666518056446206563E3", -92666518056446206563E3);
1205 verify("-42823146028335318693e-128", -42823146028335318693e-128);
1206 verify("90054602635948575728E72", 90054602635948575728E72);
1207 verify(
1208 "1."
1209 "000000000000001885589208702234638701745660206917535153946435506630705583"
1210 "68373221972569761144603605635692374830246134201063722058e-309",
1211 1.00000000000000188558920870223463870174566020691753515394643550663070558368373221972569761144603605635692374830246134201063722058e-309);
1212 verify("0e9999999999999999999999999999", 0.0);
1213 verify("-2402844368454405395.2", -2402844368454405395.2);
1214 verify("2402844368454405395.2", 2402844368454405395.2);
1215 verify(
1216 "7.0420557077594588669468784357561207962098443483187940792729600000e+59",
1217 7.0420557077594588669468784357561207962098443483187940792729600000e+59);
1218 verify(
1219 "7.0420557077594588669468784357561207962098443483187940792729600000e+59",
1220 7.0420557077594588669468784357561207962098443483187940792729600000e+59);
1221 verify(
1222 "-1.7339253062092163730578609458683877051596800000000000000000000000e+42",
1223 -1.7339253062092163730578609458683877051596800000000000000000000000e+42);
1224 verify(
1225 "-2.0972622234386619214559824785284023792871122537545728000000000000e+52",
1226 -2.0972622234386619214559824785284023792871122537545728000000000000e+52);
1227 verify(
1228 "-1.0001803374372191849407179462120053338028379051879898808320000000e+57",
1229 -1.0001803374372191849407179462120053338028379051879898808320000000e+57);
1230 verify(
1231 "-1.8607245283054342363818436991534856973992070520151142825984000000e+58",
1232 -1.8607245283054342363818436991534856973992070520151142825984000000e+58);
1233 verify(
1234 "-1.9189205311132686907264385602245237137907390376574976000000000000e+52",
1235 -1.9189205311132686907264385602245237137907390376574976000000000000e+52);
1236 verify(
1237 "-2.8184483231688951563253238886553506793085187889855201280000000000e+54",
1238 -2.8184483231688951563253238886553506793085187889855201280000000000e+54);
1239 verify(
1240 "-1.7664960224650106892054063261344555646357024359107788800000000000e+53",
1241 -1.7664960224650106892054063261344555646357024359107788800000000000e+53);
1242 verify(
1243 "-2.1470977154320536489471030463761883783915110400000000000000000000e+45",
1244 -2.1470977154320536489471030463761883783915110400000000000000000000e+45);
1245 verify(
1246 "-4.4900312744003159009338275160799498340862630046359789166919680000e+61",
1247 -4.4900312744003159009338275160799498340862630046359789166919680000e+61);
1248 verify("1", 1.0);
1249 verify("1.797693134862315700000000000000001e308", 1.7976931348623157e308);
1250 verify("3e-324", 0x0.0000000000001p-1022);
1251 verify("1.00000006e+09", 0x1.dcd651ep+29);
1252 verify("4.9406564584124653e-324", 0x0.0000000000001p-1022);
1253 verify("4.9406564584124654e-324", 0x0.0000000000001p-1022);
1254 verify("2.2250738585072009e-308", 0x0.fffffffffffffp-1022);
1255 verify("2.2250738585072014e-308", 0x1p-1022);
1256 verify("1.7976931348623157e308", 0x1.fffffffffffffp+1023);
1257 verify("1.7976931348623158e308", 0x1.fffffffffffffp+1023);
1258 verify("4503599627370496.5", 4503599627370496.5);
1259 verify("4503599627475352.5", 4503599627475352.5);
1260 verify("4503599627475353.5", 4503599627475353.5);
1261 verify("2251799813685248.25", 2251799813685248.25);
1262 verify("1125899906842624.125", 1125899906842624.125);
1263 verify("1125899906842901.875", 1125899906842901.875);
1264 verify("2251799813685803.75", 2251799813685803.75);
1265 verify("4503599627370497.5", 4503599627370497.5);
1266 verify("45035996.273704995", 45035996.273704995);
1267 verify("45035996.273704985", 45035996.273704985);
1268 verify(
1269 "0."
1270 "000000000000000000000000000000000000000000000000000000000000000000000000"
1271 "000000000000000000000000000000000000000000000000000000000000000000000000"
1272 "000000000000000000000000000000000000000000000000000000000000000000000000"
1273 "000000000000000000000000000000000000000000000000000000000000000000000000"
1274 "000000000000000000044501477170144022721148195934182639518696390927032912"
1275 "960468522194496444440421538910330590478162701758282983178260792422137401"
1276 "728773891892910553144148156412434867599762821265346585071045737627442980"
1277 "259622449029037796981144446145705102663115100318287949527959668236039986"
1278 "479250965780342141637013812613333119898765515451440315261253813266652951"
1279 "306000184917766328660755595837392240989947807556594098101021612198814605"
1280 "258742579179000071675999344145086087205681577915435923018910334964869420"
1281 "614052182892431445797605163650903606514140377217442262561590244668525767"
1282 "372446430075513332450079650686719491377688478005309963967709758965844137"
1283 "894433796621993967316936280457084866613206797017728916080020698679408551"
1284 "343728867675409720757232455434770912461317493580281734466552734375",
1285 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044501477170144022721148195934182639518696390927032912960468522194496444440421538910330590478162701758282983178260792422137401728773891892910553144148156412434867599762821265346585071045737627442980259622449029037796981144446145705102663115100318287949527959668236039986479250965780342141637013812613333119898765515451440315261253813266652951306000184917766328660755595837392240989947807556594098101021612198814605258742579179000071675999344145086087205681577915435923018910334964869420614052182892431445797605163650903606514140377217442262561590244668525767372446430075513332450079650686719491377688478005309963967709758965844137894433796621993967316936280457084866613206797017728916080020698679408551343728867675409720757232455434770912461317493580281734466552734375);
1286 verify(
1287 "0."
1288 "000000000000000000000000000000000000000000000000000000000000000000000000"
1289 "000000000000000000000000000000000000000000000000000000000000000000000000"
1290 "000000000000000000000000000000000000000000000000000000000000000000000000"
1291 "000000000000000000000000000000000000000000000000000000000000000000000000"
1292 "000000000000000000022250738585072008890245868760858598876504231122409594"
1293 "654935248025624400092282356951787758888037591552642309780950434312085877"
1294 "387158357291821993020294379224223559819827501242041788969571311791082261"
1295 "043971979604000454897391938079198936081525613113376149842043271751033627"
1296 "391549782731594143828136275113838604094249464942286316695429105080201815"
1297 "926642134996606517803095075913058719846423906068637102005108723282784678"
1298 "843631944515866135041223479014792369585208321597621066375401613736583044"
1299 "193603714778355306682834535634005074073040135602968046375918583163124224"
1300 "521599262546494300836851861719422417646455137135420132217031370496583210"
1301 "154654068035397417906022589503023501937519773030945763173210852507299305"
1302 "089761582519159720757232455434770912461317493580281734466552734375",
1303 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375);
1304 verify(
1305 "143845666314139027352611820764223558118322784524633123116263665379036815"
1306 "209139419693036582863468763794815794077659918279138752713535303473835713"
1307 "411031060945569390082419354977279201654318268051974058035436546798544018"
1308 "359870131225762454556233139701832992861319612559027418772007391481806253"
1309 "083031653315809862498411888929828137181228878953731059903752911341543873"
1310 "895489475212472498306724110876448834645437669901867307840475112141480493"
1311 "722424080599312381693232622368309077056159757045779393298582616260425588"
1312 "452913412639628220212652625338938342180672795458852559611437980126909409"
1313 "632980505480308929973699687095125857301087740440745195384669860919821392"
1314 "688269207855703322826525930548119852605981316446918758669325733577952202"
1315 "040764549868426333992190522755661669812996741289128223168550466067127792"
1316 "719829000982468018631975097866573457668378425580226970891736171946604317"
1317 "520115884909788137047711185017157986905601606166617302905958843377601564"
1318 "443970505037755427769614392827809345379280384625271596601673322264644238"
1319 "289212394005244134682242972159388437821255870100435692424303005951748934"
1320 "664657772462249891975259738209522250031112418182351225107135618176937657"
1321 "765139002829779615620881537508915912839494571051586133448626710179749711"
1322 "112590927250519479287088961717975870344260801614334326215999814970060659"
1323 "779253557445756042922697427344363032381874773077131676339857211087495998"
1324 "192373246307688452867739265415001026982223940199342748237651323138921235"
1325 "358357356637691557265091686655361236618737895955498356671276709337290603"
1326 "018897622016905802535497362221166650454931695827188097569714354656446980"
1327 "679135870731887307570838334500409015197406832583817753126695417740666139"
1328 "2229801349994695941509935655355652985723782153570084089560139142231."
1329 "738475042362596875449154552392299548947138162081694168675340677843807613"
1330 "129780449323363759027012972466987370921816813162658754726545121090545507"
1331 "240267000456594786540949605260722461937870630634874991729398208026467698"
1332 "131898691830012167897399682179601734569071423681e-733",
1333 std::numeric_limits<double>::infinity(), std::errc::result_out_of_range);
1334 verify("-2240084132271013504.131248280843119943687942846658579428",
1335 -0x1.f1660a65b00bfp+60);
1336 }
1337
1338 TEST_CASE("double.decimal_point") {
1339 constexpr auto options = [] {
1340 fast_float::parse_options ret{};
1341 ret.decimal_point = ',';
1342 return ret;
1343 }();
1344
1345 // infinities
1346 verify_options("1,8e308", std::numeric_limits<double>::infinity(),
1347 std::errc::result_out_of_range);
1348 verify_options("1,832312213213213232132132143451234453123412321321312e308",
1349 std::numeric_limits<double>::infinity(),
1350 std::errc::result_out_of_range);
1351 verify_options("2e30000000000000000", std::numeric_limits<double>::infinity(),
1352 std::errc::result_out_of_range);
1353 verify_options("2e3000", std::numeric_limits<double>::infinity(),
1354 std::errc::result_out_of_range);
1355 verify_options("1,9e308", std::numeric_limits<double>::infinity(),
1356 std::errc::result_out_of_range);
1357
1358 // finites
1359 verify_options("-2,2222222222223e-322", -0x1.68p-1069);
1360 verify_options("9007199254740993,0", 0x1p+53);
1361 verify_options("860228122,6654514319E+90", 0x1.92bb20990715fp+328);
1362 verify_options_runtime(append_zeros("9007199254740993,0", 1000), 0x1p+53);
1363 verify_options("1,1920928955078125e-07", 1.1920928955078125e-07);
1364 verify_options(
1365 "9355950000000000000,"
1366 "000000000000000000000000000000000018446744073709551616000001844674407370"
1367 "955161618446744073709551614073709551616184467440737095516160001844674407"
1368 "370955161660000018446744073709551616184467440737095516140737095516161844"
1369 "674407370955161600018446744073709551616018446744073709556744516161844674"
1370 "407370955161407370955161618446744073709551616000184467440737095516160184"
1371 "467440737095516116160001844674407370950018446744073709551616001844674407"
1372 "370955161600184467440737095511681644674407370955161600018440737095516160"
1373 "184467440737095516161844674407370955161600018446744075369107516016116160"
1374 "001844674407370950018446744073709551616001844674407370955161600184467440"
1375 "737095516161844674407370955161600018449551616184467440737095516160001844"
1376 "67440753691075160018446744073709",
1377 0x1.03ae05e8fca1cp+63);
1378 verify_options(
1379 "2,"
1380 "225073858507202124188701479202220329072405282794390378143031338374351073"
1381 "192441946867544064325638818513821882185024380699999477330130056498841077"
1382 "919287413419292972009704819519930679932909690427840647316820415659267286"
1383 "329336304746701233168529834221527445172608358596545663192828352447877877"
1384 "998943107797838336991592885945552137141811284582511455843192230798975043"
1385 "950868594124572308917389461693683723211913736589779777232866988403563902"
1386 "510444430354573967337065839810554204566938246584137476071559811765738776"
1387 "267476659123871999319040063173347090030127901881752034471902500280612777"
1388 "779167983910905785840064647159438105114891542827750411746821941339524666"
1389 "825034313061815878293790042053923750720833666932415800027583911188541886"
1390 "41513168478436313080237596295773983001708984375e-308",
1391 0x1.0000000000002p-1022);
1392 verify_options("1,0000000000000006661338147750939242541790008544921875",
1393 1.0000000000000007);
1394 verify_options("2,2250738585072013e-308", 2.2250738585072013e-308);
1395 verify_options(
1396 "1,"
1397 "000000000000001885589208702234638701745660206917535153946435506630705583"
1398 "68373221972569761144603605635692374830246134201063722058e-309",
1399 1.00000000000000188558920870223463870174566020691753515394643550663070558368373221972569761144603605635692374830246134201063722058e-309);
1400 verify_options("-2402844368454405395,2", -2402844368454405395.2);
1401 verify_options("2402844368454405395,2", 2402844368454405395.2);
1402 verify_options(
1403 "7,0420557077594588669468784357561207962098443483187940792729600000e+59",
1404 7.0420557077594588669468784357561207962098443483187940792729600000e+59);
1405 verify_options(
1406 "7,0420557077594588669468784357561207962098443483187940792729600000e+59",
1407 7.0420557077594588669468784357561207962098443483187940792729600000e+59);
1408 verify_options(
1409 "-1,7339253062092163730578609458683877051596800000000000000000000000e+42",
1410 -1.7339253062092163730578609458683877051596800000000000000000000000e+42);
1411 verify_options(
1412 "-2,0972622234386619214559824785284023792871122537545728000000000000e+52",
1413 -2.0972622234386619214559824785284023792871122537545728000000000000e+52);
1414 verify_options(
1415 "-1,0001803374372191849407179462120053338028379051879898808320000000e+57",
1416 -1.0001803374372191849407179462120053338028379051879898808320000000e+57);
1417 verify_options(
1418 "-1,8607245283054342363818436991534856973992070520151142825984000000e+58",
1419 -1.8607245283054342363818436991534856973992070520151142825984000000e+58);
1420 verify_options(
1421 "-1,9189205311132686907264385602245237137907390376574976000000000000e+52",
1422 -1.9189205311132686907264385602245237137907390376574976000000000000e+52);
1423 verify_options(
1424 "-2,8184483231688951563253238886553506793085187889855201280000000000e+54",
1425 -2.8184483231688951563253238886553506793085187889855201280000000000e+54);
1426 verify_options(
1427 "-1,7664960224650106892054063261344555646357024359107788800000000000e+53",
1428 -1.7664960224650106892054063261344555646357024359107788800000000000e+53);
1429 verify_options(
1430 "-2,1470977154320536489471030463761883783915110400000000000000000000e+45",
1431 -2.1470977154320536489471030463761883783915110400000000000000000000e+45);
1432 verify_options(
1433 "-4,4900312744003159009338275160799498340862630046359789166919680000e+61",
1434 -4.4900312744003159009338275160799498340862630046359789166919680000e+61);
1435 verify_options("1", 1.0);
1436 verify_options("1,797693134862315700000000000000001e308",
1437 1.7976931348623157e308);
1438 verify_options("3e-324", 0x0.0000000000001p-1022);
1439 verify_options("1,00000006e+09", 0x1.dcd651ep+29);
1440 verify_options("4,9406564584124653e-324", 0x0.0000000000001p-1022);
1441 verify_options("4,9406564584124654e-324", 0x0.0000000000001p-1022);
1442 verify_options("2,2250738585072009e-308", 0x0.fffffffffffffp-1022);
1443 verify_options("2,2250738585072014e-308", 0x1p-1022);
1444 verify_options("1,7976931348623157e308", 0x1.fffffffffffffp+1023);
1445 verify_options("1,7976931348623158e308", 0x1.fffffffffffffp+1023);
1446 verify_options("4503599627370496,5", 4503599627370496.5);
1447 verify_options("4503599627475352,5", 4503599627475352.5);
1448 verify_options("4503599627475353,5", 4503599627475353.5);
1449 verify_options("2251799813685248,25", 2251799813685248.25);
1450 verify_options("1125899906842624,125", 1125899906842624.125);
1451 verify_options("1125899906842901,875", 1125899906842901.875);
1452 verify_options("2251799813685803,75", 2251799813685803.75);
1453 verify_options("4503599627370497,5", 4503599627370497.5);
1454 verify_options("45035996,273704995", 45035996.273704995);
1455 verify_options("45035996,273704985", 45035996.273704985);
1456 verify_options(
1457 "0,"
1458 "000000000000000000000000000000000000000000000000000000000000000000000000"
1459 "000000000000000000000000000000000000000000000000000000000000000000000000"
1460 "000000000000000000000000000000000000000000000000000000000000000000000000"
1461 "000000000000000000000000000000000000000000000000000000000000000000000000"
1462 "000000000000000000044501477170144022721148195934182639518696390927032912"
1463 "960468522194496444440421538910330590478162701758282983178260792422137401"
1464 "728773891892910553144148156412434867599762821265346585071045737627442980"
1465 "259622449029037796981144446145705102663115100318287949527959668236039986"
1466 "479250965780342141637013812613333119898765515451440315261253813266652951"
1467 "306000184917766328660755595837392240989947807556594098101021612198814605"
1468 "258742579179000071675999344145086087205681577915435923018910334964869420"
1469 "614052182892431445797605163650903606514140377217442262561590244668525767"
1470 "372446430075513332450079650686719491377688478005309963967709758965844137"
1471 "894433796621993967316936280457084866613206797017728916080020698679408551"
1472 "343728867675409720757232455434770912461317493580281734466552734375",
1473 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044501477170144022721148195934182639518696390927032912960468522194496444440421538910330590478162701758282983178260792422137401728773891892910553144148156412434867599762821265346585071045737627442980259622449029037796981144446145705102663115100318287949527959668236039986479250965780342141637013812613333119898765515451440315261253813266652951306000184917766328660755595837392240989947807556594098101021612198814605258742579179000071675999344145086087205681577915435923018910334964869420614052182892431445797605163650903606514140377217442262561590244668525767372446430075513332450079650686719491377688478005309963967709758965844137894433796621993967316936280457084866613206797017728916080020698679408551343728867675409720757232455434770912461317493580281734466552734375);
1474 verify_options(
1475 "0,"
1476 "000000000000000000000000000000000000000000000000000000000000000000000000"
1477 "000000000000000000000000000000000000000000000000000000000000000000000000"
1478 "000000000000000000000000000000000000000000000000000000000000000000000000"
1479 "000000000000000000000000000000000000000000000000000000000000000000000000"
1480 "000000000000000000022250738585072008890245868760858598876504231122409594"
1481 "654935248025624400092282356951787758888037591552642309780950434312085877"
1482 "387158357291821993020294379224223559819827501242041788969571311791082261"
1483 "043971979604000454897391938079198936081525613113376149842043271751033627"
1484 "391549782731594143828136275113838604094249464942286316695429105080201815"
1485 "926642134996606517803095075913058719846423906068637102005108723282784678"
1486 "843631944515866135041223479014792369585208321597621066375401613736583044"
1487 "193603714778355306682834535634005074073040135602968046375918583163124224"
1488 "521599262546494300836851861719422417646455137135420132217031370496583210"
1489 "154654068035397417906022589503023501937519773030945763173210852507299305"
1490 "089761582519159720757232455434770912461317493580281734466552734375",
1491 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375);
1492 }
1493
1494 TEST_CASE("float.inf") {
1495 verify("INF", std::numeric_limits<float>::infinity());
1496 verify("-INF", -std::numeric_limits<float>::infinity());
1497 verify("INFINITY", std::numeric_limits<float>::infinity());
1498 verify("-INFINITY", -std::numeric_limits<float>::infinity());
1499 verify("infinity", std::numeric_limits<float>::infinity());
1500 verify("-infinity", -std::numeric_limits<float>::infinity());
1501 verify("inf", std::numeric_limits<float>::infinity());
1502 verify("-inf", -std::numeric_limits<float>::infinity());
1503 verify("1234456789012345678901234567890e9999999999999999999999999999",
1504 std::numeric_limits<float>::infinity(),
1505 std::errc::result_out_of_range);
1506 verify("2e3000", std::numeric_limits<float>::infinity(),
1507 std::errc::result_out_of_range);
1508 verify("3.5028234666e38", std::numeric_limits<float>::infinity(),
1509 std::errc::result_out_of_range);
1510 }
1511
1512 TEST_CASE("float.general") {
1513 // max
1514 verify("340282346638528859811704183484516925440", 0x1.fffffep+127f);
1515 // -max
1516 verify("-340282346638528859811704183484516925440", -0x1.fffffep+127f);
1517
1518 verify("-1e-999", -0.0f, std::errc::result_out_of_range);
1519 verify("1."
1520 "175494140627517859246175898662808184331245864732796240031385942718174"
1521 "6759860647699724722770042717456817626953125",
1522 0x1.2ced3p+0f);
1523 verify("1."
1524 "175494140627517859246175898662808184331245864732796240031385942718174"
1525 "6759860647699724722770042717456817626953125e-38",
1526 0x1.fffff8p-127f);
1527 verify_runtime(
1528 append_zeros("1."
1529 "17549414062751785924617589866280818433124586473279624003138"
1530 "59427181746759860647699724722770042717456817626953125",
1531 655),
1532 0x1.2ced3p+0f);
1533 verify_runtime(
1534 append_zeros("1."
1535 "17549414062751785924617589866280818433124586473279624003138"
1536 "59427181746759860647699724722770042717456817626953125",
1537 656),
1538 0x1.2ced3p+0f);
1539 verify_runtime(
1540 append_zeros("1."
1541 "17549414062751785924617589866280818433124586473279624003138"
1542 "59427181746759860647699724722770042717456817626953125",
1543 1000),
1544 0x1.2ced3p+0f);
1545 std::string test_string;
1546 test_string =
1547 append_zeros("1."
1548 "17549414062751785924617589866280818433124586473279624003138"
1549 "59427181746759860647699724722770042717456817626953125",
1550 655) +
1551 std::string("e-38");
1552 verify_runtime(test_string, 0x1.fffff8p-127f);
1553 test_string =
1554 append_zeros("1."
1555 "17549414062751785924617589866280818433124586473279624003138"
1556 "59427181746759860647699724722770042717456817626953125",
1557 656) +
1558 std::string("e-38");
1559 verify_runtime(test_string, 0x1.fffff8p-127f);
1560 test_string =
1561 append_zeros("1."
1562 "17549414062751785924617589866280818433124586473279624003138"
1563 "59427181746759860647699724722770042717456817626953125",
1564 1000) +
1565 std::string("e-38");
1566 verify_runtime(test_string, 0x1.fffff8p-127f);
1567 verify32(1.00000006e+09f);
1568 verify32(1.4012984643e-45f);
1569 verify32(1.1754942107e-38f);
1570 verify32(1.1754943508e-45f);
1571 verify("-0", -0.0f);
1572 verify("1090544144181609348835077142190", 0x1.b877ap+99f);
1573 verify("1.1754943508e-38", 1.1754943508e-38f);
1574 verify("30219.0830078125", 30219.0830078125f);
1575 verify("16252921.5", 16252921.5f);
1576 verify("5322519.25", 5322519.25f);
1577 verify("3900245.875", 3900245.875f);
1578 verify("1510988.3125", 1510988.3125f);
1579 verify("782262.28125", 782262.28125f);
1580 verify("328381.484375", 328381.484375f);
1581 verify("156782.0703125", 156782.0703125f);
1582 verify("85003.24609375", 85003.24609375f);
1583 verify("17419.6494140625", 17419.6494140625f);
1584 verify("15498.36376953125", 15498.36376953125f);
1585 verify("6318.580322265625", 6318.580322265625f);
1586 verify("2525.2840576171875", 2525.2840576171875f);
1587 verify("1370.9265747070312", 1370.9265747070312f);
1588 verify("936.3702087402344", 936.3702087402344f);
1589 verify("411.88682556152344", 411.88682556152344f);
1590 verify("206.50310516357422", 206.50310516357422f);
1591 verify("124.16878890991211", 124.16878890991211f);
1592 verify("50.811574935913086", 50.811574935913086f);
1593 verify("17.486443519592285", 17.486443519592285f);
1594 verify("13.91745138168335", 13.91745138168335f);
1595 verify("7.5464513301849365", 0x1.e2f90ep+2f);
1596 verify("2.687217116355896", 2.687217116355896f);
1597 verify("1.1877630352973938", 0x1.30113ep+0f);
1598 verify("0.7622503340244293", 0.7622503340244293f);
1599 verify("0.30531780421733856", 0x1.38a53ap-2f);
1600 verify("0.21791061013936996", 0x1.be47eap-3f);
1601 verify("0.09289376810193062", 0x1.7c7e2ep-4f);
1602 verify("0.03706067614257336", 0.03706067614257336f);
1603 verify("0.028068351559340954", 0.028068351559340954f);
1604 verify("0.012114629615098238", 0x1.8cf8e2p-7f);
1605 verify("0.004221370676532388", 0x1.14a6dap-8f);
1606 verify("0.002153817447833717", 0.002153817447833717f);
1607 verify("0.0015924838953651488", 0x1.a175cap-10f);
1608 verify("0.0008602388261351734", 0.0008602388261351734f);
1609 verify("0.00036393293703440577", 0x1.7d9c82p-12f);
1610 verify("0.00013746770127909258", 0.00013746770127909258f);
1611 verify("16407.9462890625", 16407.9462890625f);
1612 verify("1.1754947011469036e-38", 0x1.000006p-126f);
1613 verify("7.0064923216240854e-46", 0x1p-149f);
1614 verify("8388614.5", 8388614.5f);
1615 verify("0e9999999999999999999999999999", 0.f);
1616 verify(
1617 "4.7019774032891500318749461488889827112746622270883500860350068251e-38",
1618 4.7019774032891500318749461488889827112746622270883500860350068251e-38f);
1619 verify(
1620 "3."
1621 "141592653589793238462643383279502884197169399375105820974944592307816406"
1622 "2862089986280348253421170679",
1623 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f);
1624 verify(
1625 "2.3509887016445750159374730744444913556373311135441750430175034126e-38",
1626 2.3509887016445750159374730744444913556373311135441750430175034126e-38f);
1627 verify("1", 1.f);
1628 verify("7.0060e-46", 0.f, std::errc::result_out_of_range);
1629 verify("3.4028234664e38", 0x1.fffffep+127f);
1630 verify("3.4028234665e38", 0x1.fffffep+127f);
1631 verify("3.4028234666e38", 0x1.fffffep+127f);
1632 verify(
1633 "0."
1634 "000000000000000000000000000000000000011754943508222875079687365372222456"
1635 "778186655567720875215087517062784172594547271728515625",
1636 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625);
1637 verify(
1638 "0."
1639 "000000000000000000000000000000000000000000001401298464324817070923729583"
1640 "289916131280261941876515771757068283889791082685860601486638188362121582"
1641 "03125",
1642 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125f);
1643 verify(
1644 "0."
1645 "000000000000000000000000000000000000023509885615147285834557659820715330"
1646 "266457179855179808553659262368500061299303460771170648513361811637878417"
1647 "96875",
1648 0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875f);
1649 verify(
1650 "0."
1651 "000000000000000000000000000000000000011754942106924410754870294448492873"
1652 "488270524287458933338571745305715888704756189042655023513361811637878417"
1653 "96875",
1654 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875f);
1655 }
1656
1657 TEST_CASE("float.decimal_point") {
1658 constexpr auto options = [] {
1659 fast_float::parse_options ret{};
1660 ret.decimal_point = ',';
1661 return ret;
1662 }();
1663
1664 // infinity
1665 verify_options("3,5028234666e38", std::numeric_limits<float>::infinity(),
1666 std::errc::result_out_of_range);
1667
1668 // finites
1669 verify_options("1,"
1670 "1754941406275178592461758986628081843312458647327962400313859"
1671 "427181746759860647699724722770042717456817626953125",
1672 0x1.2ced3p+0f);
1673 verify_options("1,"
1674 "1754941406275178592461758986628081843312458647327962400313859"
1675 "427181746759860647699724722770042717456817626953125e-38",
1676 0x1.fffff8p-127f);
1677 verify_options_runtime(
1678 append_zeros("1,"
1679 "17549414062751785924617589866280818433124586473279624003138"
1680 "59427181746759860647699724722770042717456817626953125",
1681 655),
1682 0x1.2ced3p+0f);
1683 verify_options_runtime(
1684 append_zeros("1,"
1685 "17549414062751785924617589866280818433124586473279624003138"
1686 "59427181746759860647699724722770042717456817626953125",
1687 656),
1688 0x1.2ced3p+0f);
1689 verify_options_runtime(
1690 append_zeros("1,"
1691 "17549414062751785924617589866280818433124586473279624003138"
1692 "59427181746759860647699724722770042717456817626953125",
1693 1000),
1694 0x1.2ced3p+0f);
1695 std::string test_string;
1696 test_string =
1697 append_zeros("1,"
1698 "17549414062751785924617589866280818433124586473279624003138"
1699 "59427181746759860647699724722770042717456817626953125",
1700 655) +
1701 std::string("e-38");
1702 verify_options_runtime(test_string, 0x1.fffff8p-127f);
1703 test_string =
1704 append_zeros("1,"
1705 "17549414062751785924617589866280818433124586473279624003138"
1706 "59427181746759860647699724722770042717456817626953125",
1707 656) +
1708 std::string("e-38");
1709 verify_options_runtime(test_string, 0x1.fffff8p-127f);
1710 test_string =
1711 append_zeros("1,"
1712 "17549414062751785924617589866280818433124586473279624003138"
1713 "59427181746759860647699724722770042717456817626953125",
1714 1000) +
1715 std::string("e-38");
1716 verify_options_runtime(test_string, 0x1.fffff8p-127f);
1717 verify_options("1,1754943508e-38", 1.1754943508e-38f);
1718 verify_options("30219,0830078125", 30219.0830078125f);
1719 verify_options("1,1754947011469036e-38", 0x1.000006p-126f);
1720 verify_options("7,0064923216240854e-46", 0x1p-149f);
1721 verify_options("8388614,5", 8388614.5f);
1722 verify_options("0e9999999999999999999999999999", 0.f);
1723 verify_options(
1724 "4,7019774032891500318749461488889827112746622270883500860350068251e-38",
1725 4.7019774032891500318749461488889827112746622270883500860350068251e-38f);
1726 verify_options(
1727 "3,"
1728 "141592653589793238462643383279502884197169399375105820974944592307816406"
1729 "2862089986280348253421170679",
1730 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f);
1731 verify_options(
1732 "2,3509887016445750159374730744444913556373311135441750430175034126e-38",
1733 2.3509887016445750159374730744444913556373311135441750430175034126e-38f);
1734 verify_options("1", 1.f);
1735 verify_options("7,0060e-46", 0.f, std::errc::result_out_of_range);
1736 verify_options("3,4028234664e38", 0x1.fffffep+127f);
1737 verify_options("3,4028234665e38", 0x1.fffffep+127f);
1738 verify_options("3,4028234666e38", 0x1.fffffep+127f);
1739 verify_options(
1740 "0,"
1741 "000000000000000000000000000000000000011754943508222875079687365372222456"
1742 "778186655567720875215087517062784172594547271728515625",
1743 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625f);
1744 verify_options(
1745 "0,"
1746 "000000000000000000000000000000000000000000001401298464324817070923729583"
1747 "289916131280261941876515771757068283889791082685860601486638188362121582"
1748 "03125",
1749 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125f);
1750 verify_options(
1751 "0,"
1752 "000000000000000000000000000000000000023509885615147285834557659820715330"
1753 "266457179855179808553659262368500061299303460771170648513361811637878417"
1754 "96875",
1755 0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875f);
1756 verify_options(
1757 "0,"
1758 "000000000000000000000000000000000000011754942106924410754870294448492873"
1759 "488270524287458933338571745305715888704756189042655023513361811637878417"
1760 "96875",
1761 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875f);
1762 }
1763
1764 #ifdef __STDCPP_FLOAT16_T__
1765 TEST_CASE("float16.inf") {
1766 verify("INF", std::numeric_limits<std::float16_t>::infinity());
1767 verify("-INF", -std::numeric_limits<std::float16_t>::infinity());
1768 verify("INFINITY", std::numeric_limits<std::float16_t>::infinity());
1769 verify("-INFINITY", -std::numeric_limits<std::float16_t>::infinity());
1770 verify("infinity", std::numeric_limits<std::float16_t>::infinity());
1771 verify("-infinity", -std::numeric_limits<std::float16_t>::infinity());
1772 verify("inf", std::numeric_limits<std::float16_t>::infinity());
1773 verify("-inf", -std::numeric_limits<std::float16_t>::infinity());
1774 verify("1234456789012345678901234567890e9999999999999999999999999999",
1775 std::numeric_limits<std::float16_t>::infinity(),
1776 std::errc::result_out_of_range);
1777 verify("2e3000", std::numeric_limits<std::float16_t>::infinity(),
1778 std::errc::result_out_of_range);
1779 verify("3.5028234666e38", std::numeric_limits<std::float16_t>::infinity(),
1780 std::errc::result_out_of_range);
1781 }
1782
1783 TEST_CASE("float16.general") {
1784 // max
1785 verify("65504", 0x1.ffcp+15f16);
1786 // -max
1787 verify("-65504", -0x1.ffcp+15f16);
1788 // min
1789 verify("0.000060975551605224609375", 0x1.ff8p-15f16);
1790 verify("6.0975551605224609375e-5", 0x1.ff8p-15f16);
1791 // denorm_min
1792 verify("0.000000059604644775390625", 0x1p-24f16);
1793 verify("5.9604644775390625e-8", 0x1p-24f16);
1794 // -min
1795 verify("-0.000060975551605224609375", -0x1.ff8p-15f16);
1796 verify("-6.0975551605224609375e-5", -0x1.ff8p-15f16);
1797 // -denorm_min
1798 verify("-0.000000059604644775390625", -0x1p-24f16);
1799 verify("-5.9604644775390625e-8", -0x1p-24f16);
1800
1801 verify("-1e-999", -0.0f16, std::errc::result_out_of_range);
1802 verify("6.0975551605224609375", 0x1.864p+2f16);
1803 verify_runtime(append_zeros("6.0975551605224609375", 655), 0x1.864p+2f16);
1804 verify_runtime(append_zeros("6.0975551605224609375", 656), 0x1.864p+2f16);
1805 verify_runtime(append_zeros("6.0975551605224609375", 1000), 0x1.864p+2f16);
1806 verify_runtime(append_zeros("6.0975551605224609375", 655) +
1807 std::string("e-5"),
1808 0x1.ff8p-15f16);
1809 verify_runtime(append_zeros("6.0975551605224609375", 656) +
1810 std::string("e-5"),
1811 0x1.ff8p-15f16);
1812 verify_runtime(append_zeros("6.0975551605224609375", 1000) +
1813 std::string("e-5"),
1814 0x1.ff8p-15f16);
1815 verify("-0", -0.0f16);
1816 // verify("1090544144181609348835077142190", 0x1.b877ap+99f16);
1817 // verify("1.1754943508e-38", 1.1754943508e-38f16);
1818 verify("30219.0830078125", 30219.0830078125f16);
1819 verify("17419.6494140625", 17419.6494140625f16);
1820 verify("15498.36376953125", 15498.36376953125f16);
1821 verify("6318.580322265625", 6318.580322265625f16);
1822 verify("2525.2840576171875", 2525.2840576171875f16);
1823 verify("1370.9265747070312", 1370.9265747070312f16);
1824 verify("936.3702087402344", 936.3702087402344f16);
1825 verify("411.88682556152344", 411.88682556152344f16);
1826 verify("206.50310516357422", 206.50310516357422f16);
1827 verify("124.16878890991211", 124.16878890991211f16);
1828 verify("50.811574935913086", 50.811574935913086f16);
1829 verify("17.486443519592285", 17.486443519592285f16);
1830 verify("13.91745138168335", 13.91745138168335f16);
1831 verify("7.5464513301849365", 0x1.e2f90ep+2f16);
1832 verify("2.687217116355896", 2.687217116355896f16);
1833 verify("1.1877630352973938", 0x1.30113ep+0f16);
1834 verify("0.7622503340244293", 0.7622503340244293f16);
1835 verify("0.30531780421733856", 0x1.38a53ap-2f16);
1836 verify("0.21791061013936996", 0x1.be47eap-3f16);
1837 verify("0.09289376810193062", 0x1.7c7e2ep-4f16);
1838 verify("0.03706067614257336", 0.03706067614257336f16);
1839 verify("0.028068351559340954", 0.028068351559340954f16);
1840 verify("0.012114629615098238", 0x1.8cf8e2p-7f16);
1841 verify("0.004221370676532388", 0x1.14a6dap-8f16);
1842 verify("0.002153817447833717", 0.002153817447833717f16);
1843 verify("0.0015924838953651488", 0x1.a175cap-10f16);
1844 verify("0.0008602388261351734", 0.0008602388261351734f16);
1845 verify("0.00036393293703440577", 0x1.7d9c82p-12f16);
1846 verify("0.00013746770127909258", 0.00013746770127909258f16);
1847 verify("16407.9462890625", 16407.9462890625f16);
1848 // verify("1.1754947011469036e-38", 0x1.000006p-126f16);
1849 // verify("7.0064923216240854e-46", 0x1p-149f16);
1850 // verify("8388614.5", 8388614.5f16);
1851 verify("0e9999999999999999999999999999", 0.f16);
1852 // verify(
1853 // "4.7019774032891500318749461488889827112746622270883500860350068251e-38",
1854 // 4.7019774032891500318749461488889827112746622270883500860350068251e-38f16);
1855 verify(
1856 "3."
1857 "141592653589793238462643383279502884197169399375105820974944592307816406"
1858 "2862089986280348253421170679",
1859 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f16);
1860 // verify(
1861 // "2.3509887016445750159374730744444913556373311135441750430175034126e-38",
1862 // 2.3509887016445750159374730744444913556373311135441750430175034126e-38f16);
1863 verify("1", 1.f16);
1864 // verify("7.0060e-46", 0.f16, std::errc::result_out_of_range);
1865 // verify("3.4028234664e38", 0x1.fffffep+127f16);
1866 // verify("3.4028234665e38", 0x1.fffffep+127f16);
1867 // verify("3.4028234666e38", 0x1.fffffep+127f16);
1868 // verify(
1869 // "0."
1870 // "000000000000000000000000000000000000011754943508222875079687365372222456"
1871 // "778186655567720875215087517062784172594547271728515625",
1872 // 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625f16);
1873 // verify(
1874 // "0."
1875 // "000000000000000000000000000000000000000000001401298464324817070923729583"
1876 // "289916131280261941876515771757068283889791082685860601486638188362121582"
1877 // "03125",
1878 // 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125f16);
1879 // verify(
1880 // "0."
1881 // "000000000000000000000000000000000000023509885615147285834557659820715330"
1882 // "266457179855179808553659262368500061299303460771170648513361811637878417"
1883 // "96875",
1884 // 0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875f16);
1885 // verify(
1886 // "0."
1887 // "000000000000000000000000000000000000011754942106924410754870294448492873"
1888 // "488270524287458933338571745305715888704756189042655023513361811637878417"
1889 // "96875",
1890 // 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875f16);
1891 }
1892 #endif
1893
1894 #ifdef __STDCPP_BFLOAT16_T__
1895 TEST_CASE("bfloat16.inf") {
1896 verify("INF", std::numeric_limits<std::bfloat16_t>::infinity());
1897 verify("-INF", -std::numeric_limits<std::bfloat16_t>::infinity());
1898 verify("INFINITY", std::numeric_limits<std::bfloat16_t>::infinity());
1899 verify("-INFINITY", -std::numeric_limits<std::bfloat16_t>::infinity());
1900 verify("infinity", std::numeric_limits<std::bfloat16_t>::infinity());
1901 verify("-infinity", -std::numeric_limits<std::bfloat16_t>::infinity());
1902 verify("inf", std::numeric_limits<std::bfloat16_t>::infinity());
1903 verify("-inf", -std::numeric_limits<std::bfloat16_t>::infinity());
1904 verify("1234456789012345678901234567890e9999999999999999999999999999",
1905 std::numeric_limits<std::bfloat16_t>::infinity(),
1906 std::errc::result_out_of_range);
1907 verify("2e3000", std::numeric_limits<std::bfloat16_t>::infinity(),
1908 std::errc::result_out_of_range);
1909 verify("3.5028234666e38", std::numeric_limits<std::bfloat16_t>::infinity(),
1910 std::errc::result_out_of_range);
1911 }
1912
1913 TEST_CASE("bfloat16.general") {
1914 // max
1915 verify("338953138925153547590470800371487866880", 0x1.fep+127bf16);
1916 // -max
1917 verify("-338953138925153547590470800371487866880", -0x1.fep+127bf16);
1918 // min
1919 verify(
1920 "0."
1921 "000000000000000000000000000000000000011754943508222875079687365372222456"
1922 "778186655567720875215087517062784172594547271728515625",
1923 0x1p-126bf16);
1924 verify("1."
1925 "175494350822287507968736537222245677818665556772087521508751706278417"
1926 "2594"
1927 "547271728515625e-38",
1928 0x1p-126bf16);
1929 // denorm_min
1930 verify("0."
1931 "000000000000000000000000000000000000000091835496157991211560057541970"
1932 "4879"
1933 "435795832466228193376178712270530013483949005603790283203125",
1934 0x1p-133bf16);
1935 verify("9."
1936 "183549615799121156005754197048794357958324662281933761787122705300134"
1937 "8394"
1938 "9005603790283203125e-41",
1939 0x1p-133bf16);
1940 // -min
1941 verify(
1942 "-0."
1943 "000000000000000000000000000000000000011754943508222875079687365372222456"
1944 "778186655567720875215087517062784172594547271728515625",
1945 -0x1p-126bf16);
1946 verify(
1947 "-1."
1948 "175494350822287507968736537222245677818665556772087521508751706278417259"
1949 "4547271728515625e-38",
1950 -0x1p-126bf16);
1951 // -denorm_min
1952 verify("-0"
1953 ".00000000000000000000000000000000000000009183549615799121156005754197"
1954 "0487"
1955 "9435795832466228193376178712270530013483949005603790283203125",
1956 -0x1p-133bf16);
1957 verify("-9"
1958 ".18354961579912115600575419704879435795832466228193376178712270530013"
1959 "4839"
1960 "49005603790283203125e-41",
1961 -0x1p-133bf16);
1962
1963 verify("-1e-999", -0.0bf16, std::errc::result_out_of_range);
1964 verify_runtime(
1965 "1."
1966 "175494350822287507968736537222245677818665556772087521508751706278417"
1967 "2594547271728515625",
1968 0x1.2cp+0bf16);
1969 verify_runtime(append_zeros("1."
1970 "175494350822287507968736537222245677818665556772"
1971 "0875215087517062784172594547271728515625",
1972 655),
1973 0x1.2cp+0bf16);
1974 verify_runtime(append_zeros("1."
1975 "175494350822287507968736537222245677818665556772"
1976 "0875215087517062784172594547271728515625",
1977 656),
1978 0x1.2cp+0bf16);
1979 verify_runtime(append_zeros("1."
1980 "175494350822287507968736537222245677818665556772"
1981 "0875215087517062784172594547271728515625",
1982 1000),
1983 0x1.2cp+0bf16);
1984 verify_runtime(append_zeros("1."
1985 "175494350822287507968736537222245677818665556772"
1986 "0875215087517062784172594547271728515625",
1987 655) +
1988 std::string("e-38"),
1989 0x1p-126bf16);
1990 verify_runtime(append_zeros("1."
1991 "175494350822287507968736537222245677818665556772"
1992 "0875215087517062784172594547271728515625",
1993 656) +
1994 std::string("e-38"),
1995 0x1p-126bf16);
1996 verify_runtime(append_zeros("1."
1997 "175494350822287507968736537222245677818665556772"
1998 "0875215087517062784172594547271728515625",
1999 1000) +
2000 std::string("e-38"),
2001 0x1p-126bf16);
2002 verify("-0", -0.0bf16);
2003 // verify("1090544144181609348835077142190", 0x1.b877ap+99bf16);
2004 // verify("1.1754943508e-38", 1.1754943508e-38bf16);
2005 verify("30219.0830078125", 30219.0830078125bf16);
2006 verify("16252921.5", 16252921.5bf16);
2007 verify("5322519.25", 5322519.25bf16);
2008 verify("3900245.875", 3900245.875bf16);
2009 verify("1510988.3125", 1510988.3125bf16);
2010 verify("782262.28125", 782262.28125bf16);
2011 verify("328381.484375", 328381.484375bf16);
2012 verify("156782.0703125", 156782.0703125bf16);
2013 verify("85003.24609375", 85003.24609375bf16);
2014 verify("17419.6494140625", 17419.6494140625bf16);
2015 verify("15498.36376953125", 15498.36376953125bf16);
2016 verify("6318.580322265625", 6318.580322265625bf16);
2017 verify("2525.2840576171875", 2525.2840576171875bf16);
2018 verify("1370.9265747070312", 1370.9265747070312bf16);
2019 verify("936.3702087402344", 936.3702087402344bf16);
2020 verify("411.88682556152344", 411.88682556152344bf16);
2021 verify("206.50310516357422", 206.50310516357422bf16);
2022 verify("124.16878890991211", 124.16878890991211bf16);
2023 verify("50.811574935913086", 50.811574935913086bf16);
2024 verify("17.486443519592285", 17.486443519592285bf16);
2025 verify("13.91745138168335", 13.91745138168335bf16);
2026 verify("7.5464513301849365", 0x1.e2f90ep+2bf16);
2027 verify("2.687217116355896", 2.687217116355896bf16);
2028 verify("1.1877630352973938", 0x1.30113ep+0bf16);
2029 verify("0.7622503340244293", 0.7622503340244293bf16);
2030 verify("0.30531780421733856", 0x1.38a53ap-2bf16);
2031 verify("0.21791061013936996", 0x1.be47eap-3bf16);
2032 verify("0.09289376810193062", 0x1.7c7e2ep-4bf16);
2033 verify("0.03706067614257336", 0.03706067614257336bf16);
2034 verify("0.028068351559340954", 0.028068351559340954bf16);
2035 verify("0.012114629615098238", 0x1.8cf8e2p-7bf16);
2036 verify("0.004221370676532388", 0x1.14a6dap-8bf16);
2037 verify("0.002153817447833717", 0.002153817447833717bf16);
2038 verify("0.0015924838953651488", 0x1.a175cap-10bf16);
2039 verify("0.0008602388261351734", 0.0008602388261351734bf16);
2040 verify("0.00036393293703440577", 0x1.7d9c82p-12bf16);
2041 verify("0.00013746770127909258", 0.00013746770127909258bf16);
2042 verify("16407.9462890625", 16407.9462890625bf16);
2043 // verify("1.1754947011469036e-38", 0x1.000006p-126bf16);
2044 // verify("7.0064923216240854e-46", 0x1p-149bf16);
2045 // verify("8388614.5", 8388614.5bf16);
2046 verify("0e9999999999999999999999999999", 0.bf16);
2047 // verify(
2048 // "4.7019774032891500318749461488889827112746622270883500860350068251e-38",
2049 // 4.7019774032891500318749461488889827112746622270883500860350068251e-38bf16);
2050 verify(
2051 "3."
2052 "141592653589793238462643383279502884197169399375105820974944592307816406"
2053 "2862089986280348253421170679",
2054 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679bf16);
2055 // verify(
2056 // "2.3509887016445750159374730744444913556373311135441750430175034126e-38",
2057 // 2.3509887016445750159374730744444913556373311135441750430175034126e-38bf16);
2058 verify("1", 1.bf16);
2059 verify("7.0060e-46", 0.bf16, std::errc::result_out_of_range);
2060 verify("3.388e+38", 0x1.fep+127bf16);
2061 verify("3.389e+38", 0x1.fep+127bf16);
2062 verify("3.390e+38", 0x1.fep+127bf16);
2063 // verify(
2064 // "0."
2065 // "000000000000000000000000000000000000011754943508222875079687365372222456"
2066 // "778186655567720875215087517062784172594547271728515625",
2067 // 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625bf16);
2068 // verify(
2069 // "0."
2070 // "000000000000000000000000000000000000000000001401298464324817070923729583"
2071 // "289916131280261941876515771757068283889791082685860601486638188362121582"
2072 // "03125",
2073 // 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125bf16);
2074 // verify(
2075 // "0."
2076 // "000000000000000000000000000000000000023509885615147285834557659820715330"
2077 // "266457179855179808553659262368500061299303460771170648513361811637878417"
2078 // "96875",
2079 // 0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875bf16);
2080 // verify(
2081 // "0."
2082 // "000000000000000000000000000000000000011754942106924410754870294448492873"
2083 // "488270524287458933338571745305715888704756189042655023513361811637878417"
2084 // "96875",
2085 // 0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875bf16);
2086 }
2087 #endif
2088
2089 template <typename Int>
2090 void verify_integer_multiplication_by_power_of_10(Int mantissa,
2091 int decimal_exponent,
2092 double expected) {
2093 const double actual =
2094 fast_float::integer_times_pow10(mantissa, decimal_exponent);
2095
2096 INFO("m * 10^e=" << mantissa << " * 10^" << decimal_exponent
2097 << "\n"
2098 " expected="
2099 << fHexAndDec(expected) << "\n"
2100 << " ..actual=" << fHexAndDec(actual) << "\n"
2101 << " expected mantissa="
2102 << iHexAndDec(get_mantissa(expected)) << "\n"
2103 << " ..actual mantissa=" << iHexAndDec(get_mantissa(actual))
2104 << "\n");
2105 CHECK_EQ(actual, expected);
2106 }
2107
2108 template <typename Int>
2109 void verify_integer_multiplication_by_power_of_10(Int mantissa,
2110 int decimal_exponent) {
2111 std::string constructed_string =
2112 std::to_string(mantissa) + "e" + std::to_string(decimal_exponent);
2113 double expected_result;
2114 const auto result = fast_float::from_chars(
2115 constructed_string.data(),
2116 constructed_string.data() + constructed_string.size(), expected_result);
2117 if (result.ec != std::errc())
2118 INFO("Failed to parse: " << constructed_string);
2119 verify_integer_multiplication_by_power_of_10(mantissa, decimal_exponent,
2120 expected_result);
2121 }
2122
2123 TEST_CASE("integer_times_pow10") {
2124 // explicitly verifying API with different types of integers
2125 verify_integer_multiplication_by_power_of_10<int8_t>(31, -1, 3.1);
2126 verify_integer_multiplication_by_power_of_10<int8_t>(-31, -1, -3.1);
2127 verify_integer_multiplication_by_power_of_10<uint8_t>(31, -1, 3.1);
2128 verify_integer_multiplication_by_power_of_10<int16_t>(31415, -4, 3.1415);
2129 verify_integer_multiplication_by_power_of_10<int16_t>(-31415, -4, -3.1415);
2130 verify_integer_multiplication_by_power_of_10<uint16_t>(31415, -4, 3.1415);
2131 verify_integer_multiplication_by_power_of_10<int32_t>(314159265, -8,
2132 3.14159265);
2133 verify_integer_multiplication_by_power_of_10<int32_t>(-314159265, -8,
2134 -3.14159265);
2135 verify_integer_multiplication_by_power_of_10<uint32_t>(3141592653, -9,
2136 3.141592653);
2137 verify_integer_multiplication_by_power_of_10<int64_t>(
2138 3141592653589793238, -18, 3.141592653589793238);
2139 verify_integer_multiplication_by_power_of_10<int64_t>(
2140 -3141592653589793238, -18, -3.141592653589793238);
2141 verify_integer_multiplication_by_power_of_10<uint64_t>(
2142 3141592653589793238, -18, 3.141592653589793238);
2143 verify_integer_multiplication_by_power_of_10<long long>(
2144 -3141592653589793238, -18, -3.141592653589793238);
2145 verify_integer_multiplication_by_power_of_10<unsigned long long>(
2146 3141592653589793238, -18, 3.141592653589793238);
2147
2148 for (int mode : {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO, FE_TONEAREST}) {
2149 fesetround(mode);
2150 INFO("fesetround(): " << std::string{round_name(mode)});
2151
2152 struct Guard {
2153 ~Guard() { fesetround(FE_TONEAREST); }
2154 } guard;
2155
2156 verify_integer_multiplication_by_power_of_10(0, 0);
2157 verify_integer_multiplication_by_power_of_10(1, 0);
2158 verify_integer_multiplication_by_power_of_10(0, 1);
2159 verify_integer_multiplication_by_power_of_10(1, 1);
2160 verify_integer_multiplication_by_power_of_10(-1, 0);
2161 verify_integer_multiplication_by_power_of_10(0, -1);
2162 verify_integer_multiplication_by_power_of_10(-1, -1);
2163 verify_integer_multiplication_by_power_of_10(-1, 1);
2164 verify_integer_multiplication_by_power_of_10(1, -1);
2165
2166 verify_integer_multiplication_by_power_of_10(
2167 49406564584124654, -340, std::numeric_limits<double>::denorm_min());
2168 verify_integer_multiplication_by_power_of_10(
2169 22250738585072014, -324, std::numeric_limits<double>::min());
2170 verify_integer_multiplication_by_power_of_10(
2171 17976931348623158, 292, std::numeric_limits<double>::max());
2172
2173 // DBL_TRUE_MIN / 2 underflows to 0
2174 verify_integer_multiplication_by_power_of_10(49406564584124654 / 2, -340,
2175 0.);
2176
2177 // DBL_TRUE_MIN / 2 + 0.0000000000000001e-324 rounds to DBL_TRUE_MIN
2178 verify_integer_multiplication_by_power_of_10(
2179 49406564584124654 / 2 + 1, -340,
2180 std::numeric_limits<double>::denorm_min());
2181
2182 // DBL_MAX + 0.0000000000000001e308 overflows to infinity
2183 verify_integer_multiplication_by_power_of_10(
2184 17976931348623158 + 1, 292, std::numeric_limits<double>::infinity());
2185 // DBL_MAX + 0.00000000000000001e308 overflows to infinity
2186 verify_integer_multiplication_by_power_of_10(
2187 179769313486231580 + 1, 291, std::numeric_limits<double>::infinity());
2188
2189 // loosely verifying correct rounding of 1 to 64 bits
2190 // worth of significant digits
2191 verify_integer_multiplication_by_power_of_10(1, 42);
2192 verify_integer_multiplication_by_power_of_10(1, -42);
2193 verify_integer_multiplication_by_power_of_10(12, 42);
2194 verify_integer_multiplication_by_power_of_10(12, -42);
2195 verify_integer_multiplication_by_power_of_10(123, 42);
2196 verify_integer_multiplication_by_power_of_10(123, -42);
2197 verify_integer_multiplication_by_power_of_10(1234, 42);
2198 verify_integer_multiplication_by_power_of_10(1234, -42);
2199 verify_integer_multiplication_by_power_of_10(12345, 42);
2200 verify_integer_multiplication_by_power_of_10(12345, -42);
2201 verify_integer_multiplication_by_power_of_10(123456, 42);
2202 verify_integer_multiplication_by_power_of_10(123456, -42);
2203 verify_integer_multiplication_by_power_of_10(1234567, 42);
2204 verify_integer_multiplication_by_power_of_10(1234567, -42);
2205 verify_integer_multiplication_by_power_of_10(12345678, 42);
2206 verify_integer_multiplication_by_power_of_10(12345678, -42);
2207 verify_integer_multiplication_by_power_of_10(123456789, 42);
2208 verify_integer_multiplication_by_power_of_10(1234567890, 42);
2209 verify_integer_multiplication_by_power_of_10(1234567890, -42);
2210 verify_integer_multiplication_by_power_of_10(12345678901, 42);
2211 verify_integer_multiplication_by_power_of_10(12345678901, -42);
2212 verify_integer_multiplication_by_power_of_10(123456789012, 42);
2213 verify_integer_multiplication_by_power_of_10(123456789012, -42);
2214 verify_integer_multiplication_by_power_of_10(1234567890123, 42);
2215 verify_integer_multiplication_by_power_of_10(1234567890123, -42);
2216 verify_integer_multiplication_by_power_of_10(12345678901234, 42);
2217 verify_integer_multiplication_by_power_of_10(12345678901234, -42);
2218 verify_integer_multiplication_by_power_of_10(123456789012345, 42);
2219 verify_integer_multiplication_by_power_of_10(123456789012345, -42);
2220 verify_integer_multiplication_by_power_of_10(1234567890123456, 42);
2221 verify_integer_multiplication_by_power_of_10(1234567890123456, -42);
2222 verify_integer_multiplication_by_power_of_10(12345678901234567, 42);
2223 verify_integer_multiplication_by_power_of_10(12345678901234567, -42);
2224 verify_integer_multiplication_by_power_of_10(123456789012345678, 42);
2225 verify_integer_multiplication_by_power_of_10(123456789012345678, -42);
2226 verify_integer_multiplication_by_power_of_10(1234567890123456789, 42);
2227 verify_integer_multiplication_by_power_of_10(1234567890123456789, -42);
2228 verify_integer_multiplication_by_power_of_10(12345678901234567890ull, 42);
2229 verify_integer_multiplication_by_power_of_10(12345678901234567890ull, -42);
2230 verify_integer_multiplication_by_power_of_10(
2231 std::numeric_limits<int64_t>::max(), 42);
2232 verify_integer_multiplication_by_power_of_10(
2233 std::numeric_limits<int64_t>::max(), -42);
2234 verify_integer_multiplication_by_power_of_10(
2235 std::numeric_limits<uint64_t>::max(), 42);
2236 verify_integer_multiplication_by_power_of_10(
2237 std::numeric_limits<uint64_t>::max(), -42);
2238 }
2239 }