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: doc/modules/ROOT/pages/charconv.adoc
+17-5Lines changed: 17 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
////
2
-
Copyright 2024 Matt Borland
2
+
Copyright 2024 - 2025 Matt Borland
3
3
Distributed under the Boost Software License, Version 1.0.
4
4
https://www.boost.org/LICENSE_1_0.txt
5
5
////
@@ -21,16 +21,22 @@ namespace decimal {
21
21
22
22
enum class chars_format : unsigned
23
23
{
24
-
scientific = 1 << 0,
25
-
fixed = 1 << 1,
26
-
hex = 1 << 2,
24
+
scientific,
25
+
fixed,
26
+
hex,
27
+
cohort_preserving_scientific,
27
28
general = fixed | scientific
28
29
};
29
30
30
31
} //namespace decimal
31
32
} //namespace boost
32
33
----
33
34
35
+
The one difference here between `<charconv>` and what we provide is the option `cohort_preserving_scientific`.
36
+
This format is allowed in both `from_chars` and `to_chars` in specially defined ways to allow for cohorts to be preserved during the conversion process.
37
+
38
+
IMPORTANT: When using `from_chars` or `to_chars` with fast types it is invalid to specify the format `cohort_preserving_scientific` as these types have no support for cohorts.
When using the format `cohort_preserving_scientific` the number of digits in the string to be converted must not exceed the precision of the type that it is being converted to.
116
+
For example: `4.99999999999e+03` is invalid if being converted to `decimal32_t` because it is not exactly representable in the type.
117
+
109
118
IMPORTANT: If `std::chars_format` is used the function will return a `std::from_chars_result` and if `boost::decimal::chars_format` is used *OR* no format is specified then a `boost::decimal::from_chars_result` will be returned.
110
119
111
120
[#to_chars]
@@ -140,7 +149,10 @@ constexpr std::to_chars_result to_chars(char* first, char* last, DecimalType val
140
149
} //namespace boost
141
150
----
142
151
143
-
All `to_chars` functions ignore the effects of cohorts, and instead output normalized values.
152
+
All `to_chars` functions, except for when using the format `cohort_preserving_scientific`, ignore the effects of cohorts, and instead output normalized values.
153
+
Additionally, when using the format `cohort_preserving_scientific` if a precision is specified the function will return `{first, std::errc::invalid_argument}`.
154
+
Per IEEE 754 specifying both a precision and cohort preservation is an invalid operation.
155
+
This follows because you are potentially rounding the number, or adding trailing zeros to the fraction which both destroy the cohort information.
144
156
145
157
IMPORTANT: Same as `from_chars`, `boost::decimal::to_chars` will return a `std::to_chars_result` if `std::chars_format` is used to specify the format; otherwise it returns a `boost::decimal::to_chars_result`.
0 commit comments