Skip to content

Commit 8b849eb

Browse files
committed
Documentation regarding the integer types
1 parent ede1d6b commit 8b849eb

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
[![Ubuntu 22.04 CI (GCC 11)](https://github.com/fastfloat/fast_float/actions/workflows/ubuntu22.yml/badge.svg)](https://github.com/fastfloat/fast_float/actions/workflows/ubuntu22.yml)
55

66
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
98
round to even). In our experience, these `fast_float` functions many times faster than comparable number-parsing functions from existing C++ standard libraries.
109

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):
1211

1312
```C++
1413
from_chars_result from_chars(const char* first, const char* last, float& value, ...);
1514
from_chars_result from_chars(const char* first, const char* last, double& value, ...);
1615
```
1716
17+
You can also parse integer types:
18+
19+
20+
21+
1822
The return type (`from_chars_result`) is defined as the struct:
1923
```C++
2024
struct from_chars_result {
@@ -103,6 +107,43 @@ We support Visual Studio, macOS, Linux, freeBSD. We support big and little endia
103107
104108
We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`).
105109
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+
106147
## C++20: compile-time evaluation (constexpr)
107148

108149
In C++20, you may use `fast_float::from_chars` to parse strings

0 commit comments

Comments
 (0)