@@ -124,8 +124,8 @@ def operatorComment(operator, fixedWidth):
124
124
///
125
125
/// - Note: Overflow checking is not performed in `-Ounchecked` builds.
126
126
///
127
- /// If you want to opt out of overflow checking and ignore any overflow, use
128
- /// the overflow addition operator (`&+`).
127
+ /// If you want to opt out of overflow checking and wrap the result in case
128
+ /// of any overflow, use the overflow addition operator (`&+`).
129
129
///
130
130
/// x &+ 120 // -115
131
131
///
@@ -161,8 +161,8 @@ def operatorComment(operator, fixedWidth):
161
161
///
162
162
/// - Note: Overflow checking is not performed in `-Ounchecked` builds.
163
163
///
164
- /// If you want to opt out of overflow checking and ignore any overflow, use
165
- /// the overflow subtraction operator (`&-`).
164
+ /// If you want to opt out of overflow checking and wrap the result in case
165
+ /// of any overflow, use the overflow subtraction operator (`&-`).
166
166
///
167
167
/// x &- 50 // 227
168
168
///
@@ -198,8 +198,8 @@ def operatorComment(operator, fixedWidth):
198
198
///
199
199
/// - Note: Overflow checking is not performed in `-Ounchecked` builds.
200
200
///
201
- /// If you want to opt out of overflow checking and ignore any overflow, use
202
- /// the overflow multiplication operator (`&*`).
201
+ /// If you want to opt out of overflow checking and wrap the result in case
202
+ /// of any overflow, use the overflow multiplication operator (`&*`).
203
203
///
204
204
/// x &* 21 // -115
205
205
///
@@ -224,7 +224,7 @@ def operatorComment(operator, fixedWidth):
224
224
/// Returns the remainder of dividing the first value by the second.
225
225
///
226
226
/// The result of the remainder operator (`%`) has the same sign as `lhs` and
227
- /// is less than `rhs.magnitude`.
227
+ /// has a magnitude less than `rhs.magnitude`.
228
228
///
229
229
/// let x = 22 % 5
230
230
/// // x == 2
@@ -241,52 +241,76 @@ def operatorComment(operator, fixedWidth):
241
241
/// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
242
242
""" ,
243
243
'&+ ': """ \
244
- /// Returns the sum of the two given values, discarding any overflow.
244
+ /// Returns the sum of the two given values, wrapping the result in case of
245
+ /// any overflow.
245
246
///
246
- /// The masking addition operator (`&+`) silently discards any overflow that
247
- /// occurs during the operation. In the following example, the sum of `100`
248
- /// and `121` is greater than the maximum representable `Int8` value, so the
249
- /// result is the overflowed value:
247
+ /// The overflow addition operator (`&+`) discards any bits that overflow the
248
+ /// fixed width of the integer type. In the following example, the sum of
249
+ /// `100` and `121` is greater than the maximum representable `Int8` value,
250
+ /// so the result is the partial value after discarding the overflowing
251
+ /// bits.
250
252
///
251
253
/// let x: Int8 = 10 &+ 21
252
254
/// // x == 31
253
255
/// let y: Int8 = 100 &+ 121
254
256
/// // y == -35 (after overflow)
255
257
///
258
+ /// For more about arithmetic with overflow operators, see [Overflow
259
+ /// Operators][overflow] in *[The Swift Programming Language][tspl]*.
260
+ ///
261
+ /// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
262
+ /// [tspl]: https://docs.swift.org/swift-book/
263
+ ///
256
264
/// - Parameters:
257
265
/// - lhs: The first value to add.
258
266
/// - rhs: The second value to add.
259
267
""" ,
260
268
'&- ': """ \
261
- /// Returns the difference of the two given values, discarding any overflow.
269
+ /// Returns the difference of the two given values, wrapping the result in
270
+ /// case of any overflow.
262
271
///
263
- /// The masking subtraction operator (`&-`) silently discards any overflow
264
- /// that occurs during the operation . In the following example, the
272
+ /// The overflow subtraction operator (`&-`) discards any bits that overflow
273
+ /// the fixed width of the integer type . In the following example, the
265
274
/// difference of `10` and `21` is less than zero, the minimum representable
266
- /// `UInt` value, so the result is the overflowed value:
275
+ /// `UInt` value, so the result is the partial value after discarding the
276
+ /// overflowing bits.
267
277
///
268
278
/// let x: UInt8 = 21 &- 10
269
279
/// // x == 11
270
280
/// let y: UInt8 = 10 &- 21
271
281
/// // y == 245 (after overflow)
272
282
///
283
+ /// For more about arithmetic with overflow operators, see [Overflow
284
+ /// Operators][overflow] in *[The Swift Programming Language][tspl]*.
285
+ ///
286
+ /// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
287
+ /// [tspl]: https://docs.swift.org/swift-book/
288
+ ///
273
289
/// - Parameters:
274
290
/// - lhs: A numeric value.
275
291
/// - rhs: The value to subtract from `lhs`.
276
292
""" ,
277
293
'&* ': """ \
278
- /// Returns the product of the two given values, discarding any overflow.
294
+ /// Returns the product of the two given values, wrapping the result in case
295
+ /// of any overflow.
279
296
///
280
- /// The masking multiplication operator (`&*`) silently discards any overflow
281
- /// that occurs during the operation. In the following example, the product
282
- /// of `10` and `50` is greater than the maximum representable `Int8` value,
283
- /// so the result is the overflowed value:
297
+ /// The overflow multiplication operator (`&*`) discards any bits that
298
+ /// overflow the fixed width of the integer type. In the following example,
299
+ /// the product of `10` and `50` is greater than the maximum representable
300
+ /// `Int8` value, so the result is the partial value after discarding the
301
+ /// overflowing bits.
284
302
///
285
303
/// let x: Int8 = 10 &* 5
286
304
/// // x == 50
287
305
/// let y: Int8 = 10 &* 50
288
306
/// // y == -12 (after overflow)
289
307
///
308
+ /// For more about arithmetic with overflow operators, see [Overflow
309
+ /// Operators][overflow] in *[The Swift Programming Language][tspl]*.
310
+ ///
311
+ /// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
312
+ /// [tspl]: https://docs.swift.org/swift-book/
313
+ ///
290
314
/// - Parameters:
291
315
/// - lhs: The first value to multiply.
292
316
/// - rhs: The second value to multiply.
@@ -587,7 +611,8 @@ def assignmentOperatorComment(operator, fixedWidth):
587
611
/// Divides the first value by the second and stores the remainder in the
588
612
/// left-hand-side variable.
589
613
///
590
- /// The result has the same sign as `lhs` and is less than `rhs.magnitude`.
614
+ /// The result has the same sign as `lhs` and has a magnitude less than
615
+ /// `rhs.magnitude`.
591
616
///
592
617
/// var x = 22
593
618
/// x %= 5
@@ -606,13 +631,14 @@ def assignmentOperatorComment(operator, fixedWidth):
606
631
/// - rhs: The value to divide `lhs` by. `rhs` must not be zero.
607
632
""" ,
608
633
'&+ ': """ \
609
- /// Adds two values and stores the result in the left-hand-side variable,
610
- /// discarding any overflow.
634
+ /// Adds two values and stores the result in the left-hand-side variable,
635
+ /// wrapping any overflow.
611
636
///
612
- /// The masking addition assignment operator (`&+=`) silently discards any
613
- /// overflow that occurs during the operation. In the following example, the
614
- /// sum of `100` and `121` is greater than the maximum representable `Int8`
615
- /// value, so the result is the overflowed value:
637
+ /// The masking addition assignment operator (`&+=`) silently wraps any
638
+ /// overflow that occurs during the operation. In the following example, the
639
+ /// sum of `100` and `121` is greater than the maximum representable `Int8`
640
+ /// value, so the result is the partial value after discarding the
641
+ /// overflowing bits.
616
642
///
617
643
/// var x: Int8 = 10
618
644
/// x &+= 21
@@ -621,18 +647,25 @@ def assignmentOperatorComment(operator, fixedWidth):
621
647
/// y &+= 121
622
648
/// // y == -35 (after overflow)
623
649
///
650
+ /// For more about arithmetic with overflow operators, see [Overflow
651
+ /// Operators][overflow] in *[The Swift Programming Language][tspl]*.
652
+ ///
653
+ /// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
654
+ /// [tspl]: https://docs.swift.org/swift-book/
655
+ ///
624
656
/// - Parameters:
625
657
/// - lhs: The first value to add.
626
658
/// - rhs: The second value to add.
627
659
""" ,
628
660
'&- ': """ \
629
661
/// Subtracts the second value from the first and stores the difference in the
630
- /// left-hand-side variable, discarding any overflow.
662
+ /// left-hand-side variable, wrapping any overflow.
631
663
///
632
- /// The masking subtraction assignment operator (`&-=`) silently discards any
664
+ /// The masking subtraction assignment operator (`&-=`) silently wraps any
633
665
/// overflow that occurs during the operation. In the following example, the
634
666
/// difference of `10` and `21` is less than zero, the minimum representable
635
- /// `UInt` value, so the result is the overflowed value:
667
+ /// `UInt` value, so the result is the result is the partial value after
668
+ /// discarding the overflowing bits.
636
669
///
637
670
/// var x: Int8 = 21
638
671
/// x &-= 10
@@ -641,18 +674,25 @@ def assignmentOperatorComment(operator, fixedWidth):
641
674
/// y &-= 21
642
675
/// // y == 245 (after overflow)
643
676
///
677
+ /// For more about arithmetic with overflow operators, see [Overflow
678
+ /// Operators][overflow] in *[The Swift Programming Language][tspl]*.
679
+ ///
680
+ /// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
681
+ /// [tspl]: https://docs.swift.org/swift-book/
682
+ ///
644
683
/// - Parameters:
645
684
/// - lhs: A numeric value.
646
685
/// - rhs: The value to subtract from `lhs`.
647
686
""" ,
648
687
'&* ': """ \
649
688
/// Multiplies two values and stores the result in the left-hand-side
650
- /// variable, discarding any overflow.
689
+ /// variable, wrapping any overflow.
651
690
///
652
- /// The masking multiplication assignment operator (`&*=`) silently discards
653
- /// any overflow that occurs during the operation. In the following example,
654
- /// the product of `10` and `50` is greater than the maximum representable
655
- /// `Int8` value, so the result is the overflowed value:
691
+ /// The masking multiplication assignment operator (`&*=`) silently wraps
692
+ /// any overflow that occurs during the operation. In the following example,
693
+ /// the product of `10` and `50` is greater than the maximum representable
694
+ /// `Int8` value, so the result is the partial value after discarding the
695
+ /// overflowing bits.
656
696
///
657
697
/// var x: Int8 = 10
658
698
/// x &*= 5
@@ -661,6 +701,12 @@ def assignmentOperatorComment(operator, fixedWidth):
661
701
/// y &*= 50
662
702
/// // y == -12 (after overflow)
663
703
///
704
+ /// For more about arithmetic with overflow operators, see [Overflow
705
+ /// Operators][overflow] in *[The Swift Programming Language][tspl]*.
706
+ ///
707
+ /// [overflow]: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html#ID37
708
+ /// [tspl]: https://docs.swift.org/swift-book/
709
+ ///
664
710
/// - Parameters:
665
711
/// - lhs: The first value to multiply.
666
712
/// - rhs: The second value to multiply.
@@ -1173,6 +1219,15 @@ ${operatorComment(x.operator, False)}
1173
1219
/// declaring conformance to the protocol and ensuring that the values of your
1174
1220
/// type support negation. To customize your type's implementation, provide
1175
1221
/// your own mutating `negate()` method.
1222
+ ///
1223
+ /// When the additive inverse of a value is unrepresentable in a conforming
1224
+ /// type, the operation should either trap or return an exceptional value. For
1225
+ /// example, using the negation operator (prefix `-`) with `Int.min` results in
1226
+ /// a runtime error.
1227
+ ///
1228
+ /// let x = Int.min
1229
+ /// let y = -x
1230
+ /// // Overflow error
1176
1231
public protocol SignedNumeric : Numeric {
1177
1232
/// Returns the additive inverse of the specified value.
1178
1233
///
@@ -1201,6 +1256,14 @@ public protocol SignedNumeric : Numeric {
1201
1256
/// var x = 21
1202
1257
/// x.negate()
1203
1258
/// // x == -21
1259
+ ///
1260
+ /// The resulting value must be representable within the value's type. In
1261
+ /// particular, negating a signed, fixed-width integer type's minimum
1262
+ /// results in a value that cannot be represented.
1263
+ ///
1264
+ /// var y = Int8.min
1265
+ /// y.negate()
1266
+ /// // Overflow error
1204
1267
mutating func negate( )
1205
1268
}
1206
1269
@@ -1238,6 +1301,14 @@ extension SignedNumeric {
1238
1301
/// var x = 21
1239
1302
/// x.negate()
1240
1303
/// // x == -21
1304
+ ///
1305
+ /// The resulting value must be representable within the value's type. In
1306
+ /// particular, negating a signed, fixed-width integer type's minimum
1307
+ /// results in a value that cannot be represented.
1308
+ ///
1309
+ /// var y = Int8.min
1310
+ /// y.negate()
1311
+ /// // Overflow error
1241
1312
@inlinable // FIXME(sil-serialize-all)
1242
1313
@_transparent
1243
1314
public mutating func negate( ) {
@@ -1680,7 +1751,7 @@ ${assignmentOperatorComment(x.nonMaskingOperator, False)}
1680
1751
///
1681
1752
/// - Parameter rhs: The value to divide this value by.
1682
1753
/// - Returns: A tuple containing the quotient and remainder of this value
1683
- /// divided by `rhs`.
1754
+ /// divided by `rhs`. The remainder has the same sign as `rhs`.
1684
1755
func quotientAndRemainder( dividingBy rhs: Self )
1685
1756
-> ( quotient: Self , remainder: Self )
1686
1757
@@ -2618,7 +2689,7 @@ extension FixedWidthInteger {
2618
2689
/// // Prints "64"
2619
2690
/// // Prints "5"
2620
2691
///
2621
- /// This method is equivalent to calling the version that takes a generator,
2692
+ /// This method is equivalent to calling the version that takes a generator,
2622
2693
/// passing in the system's default random generator.
2623
2694
///
2624
2695
/// - Parameter range: The range in which to create a random value.
0 commit comments