You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -144,6 +144,39 @@ print the number 22250738585072012 three times:
144
144
std::cout << "parsed the number "<< i << std::endl;
145
145
```
146
146
147
+
## Behavior of result_out_of_range
148
+
149
+
When parsing floating-point values, the numbers can sometimes be too small (e.g., `1e-1000`) or
150
+
too large (e.g., `1e1000`). In such cases, it is customary to parse small values to zero and large
151
+
values to infinity. That is the behaviour followed by the fast_float library.
152
+
153
+
Specifically, we follow Jonathan Wakely's interpretation of the standard:
154
+
155
+
> In any case, the resulting value is one of at most two floating-point values closest to the value of the string matching the pattern. On overflow, value is set to plus or minus `std::numeric_limits<T>::max()` of the appropriate type. On underflow, value is set to a value with magnitude no greater than `std::numeric_limits<T>::min()`.
156
+
157
+
It is also the approach taken by the [Microsoft C++ library](https://github.com/microsoft/STL/blob/62205ab155d093e71dd9588a78f02c5396c3c14b/tests/std/tests/P0067R5_charconv/test.cpp#L943-L946).
158
+
159
+
Hence, we have the following examples:
160
+
161
+
```cpp
162
+
double result = -1;
163
+
std::string str = "3e-1000";
164
+
auto r = fast_float::from_chars(str.data(), str.data() + str.size(), result);
165
+
// r.ec == std::errc::result_out_of_range
166
+
// r.ptr == str.data() + 7
167
+
// result == 0
168
+
```
169
+
170
+
171
+
```cpp
172
+
double result = -1;
173
+
std::string str = "3e1000";
174
+
auto r = fast_float::from_chars(str.data(), str.data() + str.size(), result);
175
+
// r.ec == std::errc::result_out_of_range
176
+
// r.ptr == str.data() + 6
177
+
// result == std::numeric_limits<double>::infinity()
178
+
```
179
+
147
180
## C++20: compile-time evaluation (constexpr)
148
181
149
182
In C++20, you may use `fast_float::from_chars` to parse strings
0 commit comments