Skip to content

Commit 84a5790

Browse files
committed
fix the docs and simplify the implementation of unsigned wrapping ops
1 parent c66e882 commit 84a5790

File tree

1 file changed

+43
-50
lines changed

1 file changed

+43
-50
lines changed

src/libcore/num/mod.rs

+43-50
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ impl isize {
11531153
intrinsics::mul_with_overflow }
11541154
}
11551155

1156-
// `Int` + `UnsignedInt` implemented for signed integers
1156+
// `Int` + `UnsignedInt` implemented for unsigned integers
11571157
macro_rules! uint_impl {
11581158
($ActualT:ty, $BITS:expr,
11591159
$ctpop:path,
@@ -1457,8 +1457,8 @@ macro_rules! uint_impl {
14571457
/// Basic usage:
14581458
///
14591459
/// ```
1460-
/// assert_eq!((-127i8).checked_sub(1), Some(-128));
1461-
/// assert_eq!((-128i8).checked_sub(1), None);
1460+
/// assert_eq!(1u8.checked_sub(1), Some(0));
1461+
/// assert_eq!(0u8.checked_sub(1), None);
14621462
/// ```
14631463
#[stable(feature = "rust1", since = "1.0.0")]
14641464
#[inline]
@@ -1493,9 +1493,8 @@ macro_rules! uint_impl {
14931493
/// Basic usage:
14941494
///
14951495
/// ```
1496-
/// assert_eq!((-127i8).checked_div(-1), Some(127));
1497-
/// assert_eq!((-128i8).checked_div(-1), None);
1498-
/// assert_eq!((1i8).checked_div(0), None);
1496+
/// assert_eq!(128u8.checked_div(2), Some(64));
1497+
/// assert_eq!(1u8.checked_div(0), None);
14991498
/// ```
15001499
#[stable(feature = "rust1", since = "1.0.0")]
15011500
#[inline]
@@ -1591,16 +1590,15 @@ macro_rules! uint_impl {
15911590
/// Basic usage:
15921591
///
15931592
/// ```
1594-
/// assert_eq!(100i8.saturating_add(1), 101);
1595-
/// assert_eq!(100i8.saturating_add(127), 127);
1593+
/// assert_eq!(100u8.saturating_add(1), 101);
1594+
/// assert_eq!(200u8.saturating_add(127), 255);
15961595
/// ```
15971596
#[stable(feature = "rust1", since = "1.0.0")]
15981597
#[inline]
15991598
pub fn saturating_add(self, other: Self) -> Self {
16001599
match self.checked_add(other) {
1601-
Some(x) => x,
1602-
None if other >= Self::zero() => Self::max_value(),
1603-
None => Self::min_value(),
1600+
Some(x) => x,
1601+
None => Self::max_value(),
16041602
}
16051603
}
16061604

@@ -1612,16 +1610,15 @@ macro_rules! uint_impl {
16121610
/// Basic usage:
16131611
///
16141612
/// ```
1615-
/// assert_eq!(100i8.saturating_sub(127), -27);
1616-
/// assert_eq!((-100i8).saturating_sub(127), -128);
1613+
/// assert_eq!(100u8.saturating_sub(27), 73);
1614+
/// assert_eq!(13u8.saturating_sub(127), 0);
16171615
/// ```
16181616
#[stable(feature = "rust1", since = "1.0.0")]
16191617
#[inline]
16201618
pub fn saturating_sub(self, other: Self) -> Self {
16211619
match self.checked_sub(other) {
1622-
Some(x) => x,
1623-
None if other >= Self::zero() => Self::min_value(),
1624-
None => Self::max_value(),
1620+
Some(x) => x,
1621+
None => Self::min_value(),
16251622
}
16261623
}
16271624

@@ -1652,8 +1649,8 @@ macro_rules! uint_impl {
16521649
/// Basic usage:
16531650
///
16541651
/// ```
1655-
/// assert_eq!(100i8.wrapping_add(27), 127);
1656-
/// assert_eq!(100i8.wrapping_add(127), -29);
1652+
/// assert_eq!(200u8.wrapping_add(55), 255);
1653+
/// assert_eq!(200u8.wrapping_add(155), 99);
16571654
/// ```
16581655
#[stable(feature = "rust1", since = "1.0.0")]
16591656
#[inline]
@@ -1671,8 +1668,8 @@ macro_rules! uint_impl {
16711668
/// Basic usage:
16721669
///
16731670
/// ```
1674-
/// assert_eq!(0i8.wrapping_sub(127), -127);
1675-
/// assert_eq!((-2i8).wrapping_sub(127), 127);
1671+
/// assert_eq!(100u8.wrapping_sub(100), 0);
1672+
/// assert_eq!(100u8.wrapping_sub(155), 201);
16761673
/// ```
16771674
#[stable(feature = "rust1", since = "1.0.0")]
16781675
#[inline]
@@ -1690,8 +1687,8 @@ macro_rules! uint_impl {
16901687
/// Basic usage:
16911688
///
16921689
/// ```
1693-
/// assert_eq!(10i8.wrapping_mul(12), 120);
1694-
/// assert_eq!(11i8.wrapping_mul(12), -124);
1690+
/// assert_eq!(10u8.wrapping_mul(12), 120);
1691+
/// assert_eq!(25u8.wrapping_mul(12), 44);
16951692
/// ```
16961693
#[stable(feature = "rust1", since = "1.0.0")]
16971694
#[inline]
@@ -1701,68 +1698,64 @@ macro_rules! uint_impl {
17011698
}
17021699
}
17031700

1704-
/// Wrapping (modular) division. Computes `self / other`,
1705-
/// wrapping around at the boundary of the type.
1706-
///
1707-
/// The only case where such wrapping can occur is when one
1708-
/// divides `MIN / -1` on a signed type (where `MIN` is the
1709-
/// negative minimal value for the type); this is equivalent
1710-
/// to `-MIN`, a positive value that is too large to represent
1711-
/// in the type. In such a case, this function returns `MIN`
1712-
/// itself.
1701+
/// Wrapping (modular) division. Computes `self / other`.
1702+
/// Wrapped division on unsigned types is just normal division.
1703+
/// There's no way wrapping could ever happen.
1704+
/// This function exists, so that all operations
1705+
/// are accounted for in the wrapping operations.
17131706
///
17141707
/// # Examples
17151708
///
17161709
/// Basic usage:
17171710
///
17181711
/// ```
17191712
/// assert_eq!(100u8.wrapping_div(10), 10);
1720-
/// assert_eq!((-128i8).wrapping_div(-1), -128);
17211713
/// ```
17221714
#[stable(feature = "num_wrapping", since = "1.2.0")]
17231715
#[inline(always)]
17241716
pub fn wrapping_div(self, rhs: Self) -> Self {
1725-
self.overflowing_div(rhs).0
1717+
self / rhs
17261718
}
17271719

1728-
/// Wrapping (modular) remainder. Computes `self % other`,
1729-
/// wrapping around at the boundary of the type.
1730-
///
1731-
/// Such wrap-around never actually occurs mathematically;
1732-
/// implementation artifacts make `x % y` invalid for `MIN /
1733-
/// -1` on a signed type (where `MIN` is the negative
1734-
/// minimal value). In such a case, this function returns `0`.
1720+
/// Wrapping (modular) remainder. Computes `self % other`.
1721+
/// Wrapped remainder calculation on unsigned types is
1722+
/// just the regular remainder calculation.
1723+
/// There's no way wrapping could ever happen.
1724+
/// This function exists, so that all operations
1725+
/// are accounted for in the wrapping operations.
17351726
///
17361727
/// # Examples
17371728
///
17381729
/// Basic usage:
17391730
///
17401731
/// ```
17411732
/// assert_eq!(100i8.wrapping_rem(10), 0);
1742-
/// assert_eq!((-128i8).wrapping_rem(-1), 0);
17431733
/// ```
17441734
#[stable(feature = "num_wrapping", since = "1.2.0")]
17451735
#[inline(always)]
17461736
pub fn wrapping_rem(self, rhs: Self) -> Self {
1747-
self.overflowing_rem(rhs).0
1737+
self % rhs
17481738
}
17491739

17501740
/// Wrapping (modular) negation. Computes `-self`,
17511741
/// wrapping around at the boundary of the type.
17521742
///
1753-
/// The only case where such wrapping can occur is when one
1754-
/// negates `MIN` on a signed type (where `MIN` is the
1755-
/// negative minimal value for the type); this is a positive
1756-
/// value that is too large to represent in the type. In such
1757-
/// a case, this function returns `MIN` itself.
1743+
/// Since unsigned types do not have negative equivalents
1744+
/// all applications of this function will wrap (except for `-0`).
1745+
/// For values smaller than the corresponding signed type's maximum
1746+
/// the result is the same as casting the corresponding signed value.
1747+
/// Any larger values are equivalent to `MAX + 1 - (val - MAX - 1)` where
1748+
/// `MAX` is the corresponding signed type's maximum.
17581749
///
17591750
/// # Examples
17601751
///
17611752
/// Basic usage:
17621753
///
17631754
/// ```
1764-
/// assert_eq!(100i8.wrapping_neg(), -100);
1765-
/// assert_eq!((-128i8).wrapping_neg(), -128);
1755+
/// assert_eq!(100u8.wrapping_neg(), 156);
1756+
/// assert_eq!(0u8.wrapping_neg(), 0);
1757+
/// assert_eq!(180u8.wrapping_neg(), 76);
1758+
/// assert_eq!(180u8.wrapping_neg(), (127 + 1) - (180u8 - (127 + 1)));
17661759
/// ```
17671760
#[stable(feature = "num_wrapping", since = "1.2.0")]
17681761
#[inline(always)]
@@ -2002,7 +1995,7 @@ macro_rules! uint_impl {
20021995
/// Basic usage:
20031996
///
20041997
/// ```
2005-
/// assert_eq!(2i32.pow(4), 16);
1998+
/// assert_eq!(2u32.pow(4), 16);
20061999
/// ```
20072000
#[stable(feature = "rust1", since = "1.0.0")]
20082001
#[inline]

0 commit comments

Comments
 (0)