Skip to content

Commit b3222fd

Browse files
committed
Add coverage of cohort_preserving_scientific format to <charconv> page
1 parent 33b2107 commit b3222fd

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

doc/modules/ROOT/pages/charconv.adoc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
////
2-
Copyright 2024 Matt Borland
2+
Copyright 2024 - 2025 Matt Borland
33
Distributed under the Boost Software License, Version 1.0.
44
https://www.boost.org/LICENSE_1_0.txt
55
////
@@ -21,16 +21,22 @@ namespace decimal {
2121
2222
enum class chars_format : unsigned
2323
{
24-
scientific = 1 << 0,
25-
fixed = 1 << 1,
26-
hex = 1 << 2,
24+
scientific,
25+
fixed,
26+
hex,
27+
cohort_preserving_scientific,
2728
general = fixed | scientific
2829
};
2930
3031
} //namespace decimal
3132
} //namespace boost
3233
----
3334

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.
39+
3440
[#from_chars_result]
3541
== from_chars_result
3642
[source, c++]
@@ -106,6 +112,9 @@ constexpr std::from_chars_result from_chars(const char* first, const char* last,
106112
} //namespace boost
107113
----
108114

115+
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+
109118
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.
110119

111120
[#to_chars]
@@ -140,7 +149,10 @@ constexpr std::to_chars_result to_chars(char* first, char* last, DecimalType val
140149
} //namespace boost
141150
----
142151

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.
144156

145157
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`.
146158

0 commit comments

Comments
 (0)