14
14
///
15
15
/// [Wikipedia](https://en.wikipedia.org/wiki/Rounding#Rounding_to_integer)
16
16
/// provides a good overview of different rounding rules.
17
+ ///
18
+ /// Examples using rounding to integer to illustrate the various options:
19
+ /// ```
20
+ /// value | down | up | towardZero | awayFromZero |
21
+ /// =======+==============+==============+==============+==============+
22
+ /// 1.5 | 1 | 2 | 1 | 2 |
23
+ /// -------+--------------+--------------+--------------+--------------+
24
+ /// -0.5 | -1 | 0 | 0 | -1 |
25
+ /// -------+--------------+--------------+--------------+--------------+
26
+ /// 0.3 | 0 | 1 | 0 | 1 |
27
+ /// -------+--------------+--------------+--------------+--------------+
28
+ /// 2 | 2 | 2 | 2 | 2 |
29
+ /// -------+--------------+--------------+--------------+--------------+
30
+ ///
31
+ /// value | toOdd | toNearestOrAwayFromZero | toNearestOrEven |
32
+ /// =======+==============+=========================+==================+
33
+ /// 1.5 | 1 | 2 | 2 |
34
+ /// -------+--------------+-------------------------+------------------+
35
+ /// -0.5 | -1 | -1 | 0 |
36
+ /// -------+--------------+-------------------------+------------------+
37
+ /// 0.3 | 1 | 0 | 0 |
38
+ /// -------+--------------+-------------------------+------------------+
39
+ /// 2 | 2 | 2 | 2 |
40
+ /// -------+--------------+-------------------------+------------------+
41
+ ///
42
+ /// value | stochastically | requireExact |
43
+ /// =======+=======================+================+
44
+ /// 1.5 | 50% 1, 50% 2 | trap |
45
+ /// -------+-----------------------+----------------+
46
+ /// -0.5 | 50% -1, 50% 0 | trap |
47
+ /// -------+-----------------------+----------------+
48
+ /// 0.3 | 70% 0, 30% 1 | trap |
49
+ /// -------+-----------------------+----------------+
50
+ /// 2 | 2 | 2 |
51
+ /// -------+-----------------------+----------------+
52
+ /// ```
17
53
public enum RoundingRule {
18
54
/// Produces the closest representable value that is less than or equal
19
55
/// to the value being rounded.
20
56
///
21
57
/// This is the default rounding mode for integer shifts, including the
22
- /// shift operators defined in the standard library.
58
+ /// shift operators defined in the standard library.
59
+ ///
60
+ /// Examples:
61
+ /// - `(-4).divided(by: 3, rounding: .down)` is `-2`, because –2 is the
62
+ /// largest integer less than –4/3 = –1.3̅
63
+ /// - `5.shifted(rightBy: 1, rounding: .down)` is `2`, because 2 is the
64
+ /// largest integer less than 5/2 = 2.5.
23
65
case down
24
66
25
67
/// Produces the closest representable value that is greater than or equal
26
68
/// to the value being rounded.
69
+ ///
70
+ /// Examples:
71
+ /// - `(-4).divided(by: 3, rounding: .up)` is `-1`, because –1 is the
72
+ /// smallest integer greater than –4/3 = –1.3̅
73
+ /// - `5.shifted(rightBy: 1, rounding: .up)` is `3`, because 3 is the
74
+ /// smallest integer greater than 5/2 = 2.5.
27
75
case up
28
76
29
77
/// Produces the closest representable value whose magnitude is less than
30
78
/// or equal to that of the value being rounded.
79
+ ///
80
+ /// Examples:
81
+ /// - `(-4).divided(by: 3, rounding: .towardZero)` is `-1`, because –1
82
+ /// is the closest integer to –4/3 = –1.3̅ with smaller magnitude.
83
+ /// - `5.shifted(rightBy: 1, rounding: .towardZero)` is `2`, because 2
84
+ /// is the closest integer to 5/2 = 2.5 with smaller magnitude.
31
85
case towardZero
32
86
33
- /// Produces the closest representable value whose magnitude is greater than
34
- /// or equal to that of the value being rounded.
87
+ /// Produces the closest representable value whose magnitude is greater
88
+ /// than or equal to that of the value being rounded.
89
+ ///
90
+ /// Examples:
91
+ /// - `(-4).divided(by: 3, rounding: .awayFromZero)` is `-2`, because –2
92
+ /// is the closest integer to –4/3 = –1.3̅ with greater magnitude.
93
+ /// - `5.shifted(rightBy: 1, rounding: .awayFromZero)` is `3`, because 3
94
+ /// is the closest integer to 5/2 = 2.5 with greater magnitude.
35
95
case awayFromZero
36
96
37
97
/// If the value being rounded is representable, that value is returned.
@@ -45,16 +105,37 @@ public enum RoundingRule {
45
105
/// we get is the same as if we rounded directly to p₂ in the desired mode
46
106
/// so long as p₂ + 1 < p₁. Other rounding modes do not have this property,
47
107
/// and admit _double roundings_ when interoperating with some modes.
108
+ ///
109
+ /// Examples:
110
+ /// - `(-4).divided(by: 3, rounding: .toOdd)` is `-1`, because –4/3 = –1.3̅
111
+ /// is not an exact integer, and –1 is the closest odd integer.
112
+ /// - `4.shifted(rightBy: 1, rounding: .toOdd)` is `2`,
113
+ /// even though 2 is even, because 4/2 is exactly 2 and no rounding occurs.
48
114
case toOdd
49
115
50
116
/// Produces the representable value that is closest to the value being
51
117
/// rounded. If two values are equally close, the one that has greater
52
118
/// magnitude is returned.
119
+ ///
120
+ /// Examples:
121
+ /// - `(-4).divided(by: 3, rounding: .toNearestOrAwayFromZero)`
122
+ /// is `-1`, because –4/3 = –1.3̅ is closer to –1 than it is to –2.
123
+ ///
124
+ /// - `5.shifted(rightBy: 1, rounding: .toNearestOrAwayFromZero)` is `3`,
125
+ /// because 5/2 = 2.5 is equally close to 2 and 3, and 3 is further away
126
+ /// from zero.
53
127
case toNearestOrAwayFromZero
54
128
55
129
/// Produces the representable value that is closest to the value being
56
130
/// rounded. If two values are equally close, the one whose least
57
131
/// significant bit is not set is returned.
132
+ ///
133
+ /// Examples:
134
+ /// - `(-4).divided(by: 3, rounding: .toNearestOrEven)`
135
+ /// is `-1`, because –4/3 = –1.3̅ is closer to –1 than it is to –2.
136
+ ///
137
+ /// - `5.shifted(rightBy: 1, rounding: .toNearestOrEven)` is `2`,
138
+ /// because 5/2 = 2.5 is equally close to 2 and 3, and 2 is even.
58
139
case toNearestOrEven
59
140
60
141
/// Adds a uniform random value from [0, d) to the value being rounded,
@@ -88,9 +169,19 @@ public enum RoundingRule {
88
169
/// ```
89
170
/// but this isn't always possible in more sophisticated scenarios,
90
171
/// and in those cases this rounding rule may be useful.
172
+ ///
173
+ /// Examples:
174
+ /// - `(-4).divided(by: 3, rounding: .stochastically)`
175
+ /// will be –1 with probability 2/3 and –2 with probability 1/3.
176
+ /// - `5.shifted(rightBy: 1, rounding: .stochastically)`
177
+ /// will be 2 with probability 1/2 and 3 with probability 1/2.
91
178
case stochastically
92
179
93
180
/// If the value being rounded is representable, that value is returned.
94
181
/// Otherwise, a precondition failure occurs.
182
+ ///
183
+ /// Examples:
184
+ /// - `(-4).divided(by: 3, rounding: .requireExact)` will trap,
185
+ /// because –4/3 = –1.3̅ is not an integer.
95
186
case requireExact
96
187
}
0 commit comments