|
10 | 10 | [](https://github.com/irainman/fast_float/actions/workflows/vs17-clang-ci.yml) |
11 | 11 | [](https://www.codefactor.io/repository/github/irainman/fast_float) |
12 | 12 |
|
13 | | -*Note: This library is for C++ users. C programmers should consider [ffc.h](https://github.com/kolemannix/ffc.h). It is a high-performance port of fast_float to C.* |
| 13 | +*Note: This library is for C++ users. C programmers should consider [ffc.h](https://github.com/kolemannix/ffc.h). |
| 14 | +## Also you can use C++ with some additional options to maximize performance and reduce size (made by HedgehogInTheCPP, please give a star to this repo): |
14 | 15 |
|
| 16 | +There is a really common use case in mathematical and other abstract syntax tree (AST)-like parsers that already processes |
| 17 | +the sign and all other symbols before any number by itself. In this case you can use FastFloat to only parse positive numbers |
| 18 | +in all supported formats with macros `FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN`, which significantly reduce the code size |
| 19 | +and improve performance. You also can use macros `FASTFLOAT_ISNOT_CHECKED_BOUNDS` if your code already checks bounds; |
| 20 | +it's very likely because all parsers need to check the first character by itself before parsing. Additionally, you can use |
| 21 | +macros `FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED` if you only need `FE_TONEAREST` rounding mode in the parsing; this option |
| 22 | +also improves performance a bit and reduces code size. In the high-performance example, I also use the [fmt library](https://github.com/fmtlib/fmt), which also |
| 23 | +supports all C++ standards since C++11. I also recommend using `string_view` everywhere if it's possible; it's available |
| 24 | +since C++17, and if you want maximum performance, use the latest compiler with the latest C++ with maximum optimization: |
| 25 | +``` |
| 26 | +-O3 -DNDEBUG + LTO |
| 27 | +``` |
| 28 | +```C++ |
| 29 | +#define FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN |
| 30 | +#define FASTFLOAT_ISNOT_CHECKED_BOUNDS |
| 31 | +#define FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED |
| 32 | +#include "fast_float/fast_float.h" |
| 33 | +#include "fmt/base.h" |
| 34 | +#include <string_view> |
| 35 | + |
| 36 | +int main() { |
| 37 | + std::string_view input = "23.14069263277926900572"; |
| 38 | + double result; |
| 39 | + auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result); |
| 40 | + if ((answer.ec != std::errc()) || ((result != 23.14069263277927 /*properly rounded value */))) |
| 41 | + { |
| 42 | + fmt::print(stderr, "parsing failure!\n the number {}.", result); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + fmt::print("parsed the number {}.", result); |
| 46 | + return 0; |
| 47 | +} |
| 48 | +``` |
15 | 49 |
|
16 | 50 | The fast_float library provides fast header-only implementations for the C++ |
17 | 51 | from_chars functions for `float` and `double` types as well as integer types. |
@@ -392,42 +426,6 @@ int main() { |
392 | 426 | } |
393 | 427 | ``` |
394 | 428 |
|
395 | | -## You also can use some additional options to maximize performance and reduce size (made by HedgehogInTheCPP): |
396 | | - |
397 | | -There is a really common use case in mathematical and other abstract syntax tree (AST)-like parsers that already processes |
398 | | -the sign and all other symbols before any number by itself. In this case you can use FastFloat to only parse positive numbers |
399 | | -in all supported formats with macros `FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN`, which significantly reduce the code size |
400 | | -and improve performance. You also can use macros `FASTFLOAT_ISNOT_CHECKED_BOUNDS` if your code already checks bounds; |
401 | | -it's very likely because all parsers need to check the first character by itself before parsing. Additionally, you can use |
402 | | -macros `FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED` if you only need `FE_TONEAREST` rounding mode in the parsing; this option |
403 | | -also improves performance a bit and reduces code size. In the high-performance example, I also use the [fmt library](https://github.com/fmtlib/fmt), which also |
404 | | -supports all C++ standards since C++11. I also recommend using `string_view` everywhere if it's possible; it's available |
405 | | -since C++17, and if you want maximum performance, use the latest compiler with the latest C++ with maximum optimization: |
406 | | -``` |
407 | | --O3 -DNDEBUG + LTO |
408 | | -``` |
409 | | -```C++ |
410 | | -#define FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN |
411 | | -#define FASTFLOAT_ISNOT_CHECKED_BOUNDS |
412 | | -#define FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED |
413 | | -#include "fast_float/fast_float.h" |
414 | | -#include "fmt/base.h" |
415 | | -#include <string_view> |
416 | | - |
417 | | -int main() { |
418 | | - std::string_view input = "23.14069263277926900572"; |
419 | | - double result; |
420 | | - auto answer = fast_float::from_chars(input.data(), input.data() + input.size(), result); |
421 | | - if ((answer.ec != std::errc()) || ((result != 23.14069263277927 /*properly rounded value */))) |
422 | | - { |
423 | | - fmt::print(stderr, "parsing failure!\n the number {}.", result); |
424 | | - return 1; |
425 | | - } |
426 | | - fmt::print("parsed the number {}.", result); |
427 | | - return 0; |
428 | | -} |
429 | | -``` |
430 | | - |
431 | 429 | ## Multiplication of an integer by a power of 10 |
432 | 430 | An integer `W` can be multiplied by a power of ten `10^Q` and |
433 | 431 | converted to `double` with correctly rounded value |
|
0 commit comments