Skip to content

Commit 20ba149

Browse files
committed
More wordsmithing.
1 parent 98cec13 commit 20ba149

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

stdlib/public/core/FloatingPointParsing.swift.gyb

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ extension ${Self}: LosslessStringConvertible {
5656
/// hexadecimal format or special floating-point values for infinity and NaN
5757
/// ("not a number").
5858
///
59-
/// The given string may begin with a plus or minus sign character (`+` or
60-
/// `-`). The allowed formats for each of these representations is then as
61-
/// follows:
59+
/// The `text` string may begin with a plus or minus sign character (`+` or
60+
/// `-`) followed by one of the following formats:
6261
///
63-
/// - A *decimal value* contains the significand, a sequence of decimal
64-
/// digits that may include a decimal point.
62+
/// - A *decimal value* contains a significand consisting of one
63+
/// or more decimal digits that may include a decimal point:
6564
///
6665
/// let c = ${Self}("-1.0")
6766
/// // c == -1.0
@@ -78,32 +77,58 @@ extension ${Self}: LosslessStringConvertible {
7877
/// let e = ${Self}("2837.5e-2")
7978
/// // e == 28.375
8079
///
81-
/// - A *hexadecimal value* contains the significand, either `0X` or `0x`,
82-
/// followed by a sequence of hexadecimal digits. The significand may
80+
/// A decimal string is converted into a correctly-rounded ${Self}
81+
/// value using IEEE 754 round-to-nearest
82+
/// conventions. Very small values will be converted to positive or
83+
/// negative zero, very large values will be converted
84+
/// to plus or minus infinity.
85+
///
86+
/// let y = ${Self}("1.23e-9999")
87+
/// // y == 0.0
88+
/// // y?.sign == .plus
89+
///
90+
/// let z = ${Self}("-7.89e-7206")
91+
/// // z == -0.0
92+
/// // z?.sign == .minus
93+
///
94+
/// let r = ${Self}("1.23e17802")
95+
/// // r == ${Self}.infinity
96+
///
97+
/// let s = ${Self}("-7.89e7206")
98+
/// // s == ${Self}.-infinity
99+
///
100+
/// - A *hexadecimal value* contains a significand consisting of
101+
// `0X` or `0x` followed by one or more hexadecimal digits that may
83102
/// include a decimal point.
84103
///
85104
/// let f = ${Self}("0x1c.6")
86105
/// // f == 28.375
87106
///
88-
/// A hexadecimal value may also include an exponent following the
89-
/// significand, indicating the power of 2 by which the significand should
107+
/// A hexadecimal value may also include an exponent
108+
/// indicating the power of 2 by which the significand should
90109
/// be multiplied. If included, the exponent is separated by a single
91110
/// character, `p` or `P`, and consists of an optional plus or minus sign
92111
/// character and a sequence of decimal digits.
93112
///
94113
/// let g = ${Self}("0x1.c6p4")
95114
/// // g == 28.375
96115
///
97-
/// - A value of *infinity* contains one of the strings `"inf"` or
98-
/// `"infinity"`, case insensitive.
116+
/// As with decimal strings, hexadecimal strings are converted
117+
/// to the closest ${Self} value to the real number. Very small
118+
/// or very large values are converted to plus or minus zero or plus
119+
/// or minus infinity.
120+
///
121+
/// - The input strings `"inf"` or `"infinity"` (case insensitive)
122+
/// are converted to an infinite result:
99123
///
100124
/// let i = ${Self}("inf")
101125
/// // i == ${Self}.infinity
102126
///
103127
/// let j = ${Self}("-Infinity")
104128
/// // j == -${Self}.infinity
105129
///
106-
/// - A value of *NaN* contains the string `"nan"`, case insensitive.
130+
/// - An input string of `"nan"` (case insensitive) is converted
131+
/// into a *NaN* value:
107132
///
108133
/// let n = ${Self}("-nan")
109134
/// // n?.isNaN == true
@@ -120,18 +145,16 @@ extension ${Self}: LosslessStringConvertible {
120145
/// // p?.isNaN == true
121146
/// // String(p!) == "nan(0x10)"
122147
///
123-
/// Passing any other format or any additional characters as `text` results
124-
/// in `nil`. For example, the following conversions result in `nil`:
148+
/// - A value in any other format or containing additional characters
149+
/// results in a `nil` value. For example, the following conversions
150+
/// result in `nil`:
125151
///
126-
/// ${Self}(" 5.0") // Includes whitespace
127-
/// ${Self}("±2.0") // Invalid character
128-
/// ${Self}("0x1.25e4") // Incorrect exponent format
152+
/// ${Self}(" 5.0") // Includes whitespace
153+
/// ${Self}("±2.0") // Invalid character
154+
/// ${Self}("0x1.25e4") // Incorrect exponent format
129155
///
130-
/// - Parameter text: The input string to convert to a `${Self}` instance. If
131-
/// `text` has invalid characters or is in an invalid format, the result is
132-
/// `nil`. If the input is valid but the parsed value would be too small
133-
/// or large for the type, the result will be a zero or infinity with the
134-
/// appropriate sign.
156+
/// - Parameter text: The input string to convert to a `${Self}?` instance.
157+
/// The value will be `nil` if the input is incorrectly formatted.
135158
@inlinable
136159
public init?<S: StringProtocol>(_ text: S) {
137160
%if bits == 16:

0 commit comments

Comments
 (0)