|
4 | 4 | [](https://github.com/fastfloat/fast_float/actions/workflows/ubuntu22.yml)
|
5 | 5 |
|
6 | 6 | The fast_float library provides fast header-only implementations for the C++ from_chars
|
7 |
| -functions for `float` and `double` types. These functions convert ASCII strings representing |
8 |
| -decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including |
| 7 | +functions for `float` and `double` types as well as integer types. These functions convert ASCII strings representing decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including |
9 | 8 | round to even). In our experience, these `fast_float` functions many times faster than comparable number-parsing functions from existing C++ standard libraries.
|
10 | 9 |
|
11 |
| -Specifically, `fast_float` provides the following two functions with a C++17-like syntax (the library itself only requires C++11): |
| 10 | +Specifically, `fast_float` provides the following two functions to parse floating-point numbers with a C++17-like syntax (the library itself only requires C++11): |
12 | 11 |
|
13 | 12 | ```C++
|
14 | 13 | from_chars_result from_chars(const char* first, const char* last, float& value, ...);
|
15 | 14 | from_chars_result from_chars(const char* first, const char* last, double& value, ...);
|
16 | 15 | ```
|
17 | 16 |
|
| 17 | +You can also parse integer types: |
| 18 | +
|
| 19 | +
|
| 20 | +
|
| 21 | +
|
18 | 22 | The return type (`from_chars_result`) is defined as the struct:
|
19 | 23 | ```C++
|
20 | 24 | struct from_chars_result {
|
@@ -103,6 +107,43 @@ We support Visual Studio, macOS, Linux, freeBSD. We support big and little endia
|
103 | 107 |
|
104 | 108 | We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`).
|
105 | 109 |
|
| 110 | +
|
| 111 | +## Integer types |
| 112 | +
|
| 113 | +You can also parse integer types using different bases (e.g., 2, 10, 16). The following code will |
| 114 | +print the number 22250738585072012 three times: |
| 115 | +
|
| 116 | +
|
| 117 | +```C++ |
| 118 | + uint64_t i; |
| 119 | + const char str[] = "22250738585072012"; |
| 120 | + auto answer = fast_float::from_chars(str, str + strlen(str), i); |
| 121 | + if (answer.ec != std::errc()) { |
| 122 | + std::cerr << "parsing failure\n"; |
| 123 | + return EXIT_FAILURE; |
| 124 | + } |
| 125 | + std::cout << "parsed the number "<< i << std::endl; |
| 126 | +
|
| 127 | + const char binstr[] = "1001111000011001110110111001001010110100111000110001100"; |
| 128 | +
|
| 129 | + answer = fast_float::from_chars(binstr, binstr + strlen(binstr), i, 2); |
| 130 | + if (answer.ec != std::errc()) { |
| 131 | + std::cerr << "parsing failure\n"; |
| 132 | + return EXIT_FAILURE; |
| 133 | + } |
| 134 | + std::cout << "parsed the number "<< i << std::endl; |
| 135 | +
|
| 136 | +
|
| 137 | + const char hexstr[] = "4f0cedc95a718c"; |
| 138 | +
|
| 139 | + answer = fast_float::from_chars(hexstr, hexstr + strlen(hexstr), i, 16); |
| 140 | + if (answer.ec != std::errc()) { |
| 141 | + std::cerr << "parsing failure\n"; |
| 142 | + return EXIT_FAILURE; |
| 143 | + } |
| 144 | + std::cout << "parsed the number "<< i << std::endl; |
| 145 | +``` |
| 146 | + |
106 | 147 | ## C++20: compile-time evaluation (constexpr)
|
107 | 148 |
|
108 | 149 | In C++20, you may use `fast_float::from_chars` to parse strings
|
|
0 commit comments