@@ -1746,6 +1746,173 @@ macro_rules! int_impl {
1746
1746
}
1747
1747
}
1748
1748
1749
+ /// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
1750
+ ///
1751
+ /// # Panics
1752
+ ///
1753
+ /// This function will panic if `rhs` is 0 or the division results in overflow.
1754
+ ///
1755
+ /// # Examples
1756
+ ///
1757
+ /// Basic usage:
1758
+ ///
1759
+ /// ```
1760
+ /// #![feature(int_roundings)]
1761
+ #[ doc = concat!( "let a: " , stringify!( $SelfT) , " = 8;" ) ]
1762
+ /// let b = 3;
1763
+ ///
1764
+ /// assert_eq!(a.div_floor(b), 2);
1765
+ /// assert_eq!(a.div_floor(-b), -3);
1766
+ /// assert_eq!((-a).div_floor(b), -3);
1767
+ /// assert_eq!((-a).div_floor(-b), 2);
1768
+ /// ```
1769
+ #[ unstable( feature = "int_roundings" , issue = "88581" ) ]
1770
+ #[ must_use = "this returns the result of the operation, \
1771
+ without modifying the original"]
1772
+ #[ inline]
1773
+ #[ rustc_inherit_overflow_checks]
1774
+ pub const fn div_floor( self , rhs: Self ) -> Self {
1775
+ let d = self / rhs;
1776
+ let r = self % rhs;
1777
+ if ( r > 0 && rhs < 0 ) || ( r < 0 && rhs > 0 ) {
1778
+ d - 1
1779
+ } else {
1780
+ d
1781
+ }
1782
+ }
1783
+
1784
+ /// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1785
+ ///
1786
+ /// # Panics
1787
+ ///
1788
+ /// This function will panic if `rhs` is 0 or the division results in overflow.
1789
+ ///
1790
+ /// # Examples
1791
+ ///
1792
+ /// Basic usage:
1793
+ ///
1794
+ /// ```
1795
+ /// #![feature(int_roundings)]
1796
+ #[ doc = concat!( "let a: " , stringify!( $SelfT) , " = 8;" ) ]
1797
+ /// let b = 3;
1798
+ ///
1799
+ /// assert_eq!(a.div_ceil(b), 3);
1800
+ /// assert_eq!(a.div_ceil(-b), -2);
1801
+ /// assert_eq!((-a).div_ceil(b), -2);
1802
+ /// assert_eq!((-a).div_ceil(-b), 3);
1803
+ /// ```
1804
+ #[ unstable( feature = "int_roundings" , issue = "88581" ) ]
1805
+ #[ must_use = "this returns the result of the operation, \
1806
+ without modifying the original"]
1807
+ #[ inline]
1808
+ #[ rustc_inherit_overflow_checks]
1809
+ pub const fn div_ceil( self , rhs: Self ) -> Self {
1810
+ let d = self / rhs;
1811
+ let r = self % rhs;
1812
+ if ( r > 0 && rhs > 0 ) || ( r < 0 && rhs < 0 ) {
1813
+ d + 1
1814
+ } else {
1815
+ d
1816
+ }
1817
+ }
1818
+
1819
+ /// If `rhs` is positive, calculates the smallest value greater than or
1820
+ /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
1821
+ /// calculates the largest value less than or equal to `self` that is a
1822
+ /// multiple of `rhs`.
1823
+ ///
1824
+ /// # Panics
1825
+ ///
1826
+ /// This function will panic if `rhs` is 0 or the operation results in overflow.
1827
+ ///
1828
+ /// # Examples
1829
+ ///
1830
+ /// Basic usage:
1831
+ ///
1832
+ /// ```
1833
+ /// #![feature(int_roundings)]
1834
+ #[ doc = concat!( "assert_eq!(16_" , stringify!( $SelfT) , ".next_multiple_of(8), 16);" ) ]
1835
+ #[ doc = concat!( "assert_eq!(23_" , stringify!( $SelfT) , ".next_multiple_of(8), 24);" ) ]
1836
+ #[ doc = concat!( "assert_eq!(16_" , stringify!( $SelfT) , ".next_multiple_of(-8), 16);" ) ]
1837
+ #[ doc = concat!( "assert_eq!(23_" , stringify!( $SelfT) , ".next_multiple_of(-8), 16);" ) ]
1838
+ #[ doc = concat!( "assert_eq!((-16_" , stringify!( $SelfT) , ").next_multiple_of(8), -16);" ) ]
1839
+ #[ doc = concat!( "assert_eq!((-23_" , stringify!( $SelfT) , ").next_multiple_of(8), -16);" ) ]
1840
+ #[ doc = concat!( "assert_eq!((-16_" , stringify!( $SelfT) , ").next_multiple_of(-8), -16);" ) ]
1841
+ #[ doc = concat!( "assert_eq!((-23_" , stringify!( $SelfT) , ").next_multiple_of(-8), -24);" ) ]
1842
+ /// ```
1843
+ #[ unstable( feature = "int_roundings" , issue = "88581" ) ]
1844
+ #[ must_use = "this returns the result of the operation, \
1845
+ without modifying the original"]
1846
+ #[ inline]
1847
+ #[ rustc_inherit_overflow_checks]
1848
+ pub const fn next_multiple_of( self , rhs: Self ) -> Self {
1849
+ // This would otherwise fail when calculating `r` when self == T::MIN.
1850
+ if rhs == -1 {
1851
+ return self ;
1852
+ }
1853
+
1854
+ let r = self % rhs;
1855
+ let m = if ( r > 0 && rhs < 0 ) || ( r < 0 && rhs > 0 ) {
1856
+ r + rhs
1857
+ } else {
1858
+ r
1859
+ } ;
1860
+
1861
+ if m == 0 {
1862
+ self
1863
+ } else {
1864
+ self + ( rhs - m)
1865
+ }
1866
+ }
1867
+
1868
+ /// If `rhs` is positive, calculates the smallest value greater than or
1869
+ /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
1870
+ /// calculates the largest value less than or equal to `self` that is a
1871
+ /// multiple of `rhs`. Returns `None` if `rhs` is zero or the operation
1872
+ /// would result in overflow.
1873
+ ///
1874
+ /// # Examples
1875
+ ///
1876
+ /// Basic usage:
1877
+ ///
1878
+ /// ```
1879
+ /// #![feature(int_roundings)]
1880
+ #[ doc = concat!( "assert_eq!(16_" , stringify!( $SelfT) , ".checked_next_multiple_of(8), Some(16));" ) ]
1881
+ #[ doc = concat!( "assert_eq!(23_" , stringify!( $SelfT) , ".checked_next_multiple_of(8), Some(24));" ) ]
1882
+ #[ doc = concat!( "assert_eq!(16_" , stringify!( $SelfT) , ".checked_next_multiple_of(-8), Some(16));" ) ]
1883
+ #[ doc = concat!( "assert_eq!(23_" , stringify!( $SelfT) , ".checked_next_multiple_of(-8), Some(16));" ) ]
1884
+ #[ doc = concat!( "assert_eq!((-16_" , stringify!( $SelfT) , ").checked_next_multiple_of(8), Some(-16));" ) ]
1885
+ #[ doc = concat!( "assert_eq!((-23_" , stringify!( $SelfT) , ").checked_next_multiple_of(8), Some(-16));" ) ]
1886
+ #[ doc = concat!( "assert_eq!((-16_" , stringify!( $SelfT) , ").checked_next_multiple_of(-8), Some(-16));" ) ]
1887
+ #[ doc = concat!( "assert_eq!((-23_" , stringify!( $SelfT) , ").checked_next_multiple_of(-8), Some(-24));" ) ]
1888
+ #[ doc = concat!( "assert_eq!(1_" , stringify!( $SelfT) , ".checked_next_multiple_of(0), None);" ) ]
1889
+ #[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX.checked_next_multiple_of(2), None);" ) ]
1890
+ /// ```
1891
+ #[ unstable( feature = "int_roundings" , issue = "88581" ) ]
1892
+ #[ must_use = "this returns the result of the operation, \
1893
+ without modifying the original"]
1894
+ #[ inline]
1895
+ #[ rustc_inherit_overflow_checks]
1896
+ pub const fn checked_next_multiple_of( self , rhs: Self ) -> Option <Self > {
1897
+ // This would otherwise fail when calculating `r` when self == T::MIN.
1898
+ if rhs == -1 {
1899
+ return Some ( self ) ;
1900
+ }
1901
+
1902
+ let r = try_opt!( self . checked_rem( rhs) ) ;
1903
+ let m = if ( r > 0 && rhs < 0 ) || ( r < 0 && rhs > 0 ) {
1904
+ try_opt!( r. checked_add( rhs) )
1905
+ } else {
1906
+ r
1907
+ } ;
1908
+
1909
+ if m == 0 {
1910
+ Some ( self )
1911
+ } else {
1912
+ self . checked_add( try_opt!( rhs. checked_sub( m) ) )
1913
+ }
1914
+ }
1915
+
1749
1916
/// Returns the logarithm of the number with respect to an arbitrary base.
1750
1917
///
1751
1918
/// This method might not be optimized owing to implementation details;
0 commit comments