Skip to content

Commit 7f6ac30

Browse files
committed
Add rem_floor and rem_ceil
1 parent 341ef15 commit 7f6ac30

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

library/core/src/num/int_macros.rs

+81
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,45 @@ macro_rules! int_impl {
21902190
}
21912191
}
21922192

2193+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2194+
///
2195+
/// # Panics
2196+
///
2197+
/// This function will panic if `rhs` is zero.
2198+
///
2199+
/// ## Overflow behavior
2200+
///
2201+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2202+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2203+
///
2204+
/// # Examples
2205+
///
2206+
/// Basic usage:
2207+
///
2208+
/// ```
2209+
/// #![feature(int_roundings)]
2210+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2211+
/// let b = 3;
2212+
///
2213+
/// assert_eq!(a.rem_floor(b), 2);
2214+
/// assert_eq!(a.rem_floor(-b), -1);
2215+
/// assert_eq!((-a).rem_floor(b), 1);
2216+
/// assert_eq!((-a).rem_floor(-b), -2);
2217+
/// ```
2218+
#[unstable(feature = "int_roundings", issue = "88581")]
2219+
#[must_use = "this returns the result of the operation, \
2220+
without modifying the original"]
2221+
#[inline]
2222+
#[rustc_inherit_overflow_checks]
2223+
pub const fn rem_floor(self, rhs: Self) -> Self {
2224+
let r = self % rhs;
2225+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2226+
r + rhs
2227+
} else {
2228+
r
2229+
}
2230+
}
2231+
21932232
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
21942233
///
21952234
/// # Panics
@@ -2230,6 +2269,48 @@ macro_rules! int_impl {
22302269
}
22312270
}
22322271

2272+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2273+
///
2274+
/// This operation is *only* available for signed integers,
2275+
/// since the result would be negative if both operands are positive.
2276+
///
2277+
/// # Panics
2278+
///
2279+
/// This function will panic if `rhs` is zero.
2280+
///
2281+
/// ## Overflow behavior
2282+
///
2283+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2284+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2285+
///
2286+
/// # Examples
2287+
///
2288+
/// Basic usage:
2289+
///
2290+
/// ```
2291+
/// #![feature(rem_ceil)]
2292+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2293+
/// let b = 3;
2294+
///
2295+
/// assert_eq!(a.rem_ceil(b), -1);
2296+
/// assert_eq!(a.rem_ceil(-b), 2);
2297+
/// assert_eq!((-a).rem_ceil(b), -2);
2298+
/// assert_eq!((-a).rem_ceil(-b), 1);
2299+
/// ```
2300+
#[unstable(feature = "rem_ceil", issue = "88581")]
2301+
#[must_use = "this returns the result of the operation, \
2302+
without modifying the original"]
2303+
#[inline]
2304+
#[rustc_inherit_overflow_checks]
2305+
pub const fn rem_ceil(self, rhs: Self) -> Self {
2306+
let r = self % rhs;
2307+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
2308+
r - rhs
2309+
} else {
2310+
r
2311+
}
2312+
}
2313+
22332314
/// If `rhs` is positive, calculates the smallest value greater than or
22342315
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
22352316
/// calculates the largest value less than or equal to `self` that is a

library/core/src/num/uint_macros.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,32 @@ macro_rules! uint_impl {
20592059
self / rhs
20602060
}
20612061

2062+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2063+
///
2064+
/// This is the same as performing `self % rhs` for all unsigned integers.
2065+
///
2066+
/// # Panics
2067+
///
2068+
/// This function will panic if `rhs` is zero.
2069+
///
2070+
/// # Examples
2071+
///
2072+
/// Basic usage:
2073+
///
2074+
/// ```
2075+
/// #![feature(int_roundings)]
2076+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".rem_floor(4), 3);")]
2077+
/// ```
2078+
#[unstable(feature = "int_roundings", issue = "88581")]
2079+
#[must_use = "this returns the result of the operation, \
2080+
without modifying the original"]
2081+
#[inline]
2082+
#[rustc_inherit_overflow_checks]
2083+
pub const fn rem_floor(self, rhs: Self) -> Self {
2084+
self % rhs
2085+
}
2086+
2087+
20622088
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
20632089
///
20642090
/// # Panics
@@ -2086,13 +2112,51 @@ macro_rules! uint_impl {
20862112
pub const fn div_ceil(self, rhs: Self) -> Self {
20872113
let d = self / rhs;
20882114
let r = self % rhs;
2089-
if r > 0 && rhs > 0 {
2115+
if r != 0 {
20902116
d + 1
20912117
} else {
20922118
d
20932119
}
20942120
}
20952121

2122+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2123+
///
2124+
/// Since this remainder can never be positive, we return the opposite of the actual remainder.
2125+
/// If you want the sign to reflect the actual remainder, you need to use the [signed version].
2126+
///
2127+
#[doc = concat!("[signed version]: primitive.", stringify!($SignedT), ".html#method.rem_ceil")]
2128+
///
2129+
/// # Panics
2130+
///
2131+
/// This function will panic if `rhs` is zero.
2132+
///
2133+
/// ## Overflow behavior
2134+
///
2135+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2136+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2137+
///
2138+
/// # Examples
2139+
///
2140+
/// Basic usage:
2141+
///
2142+
/// ```
2143+
/// #![feature(rem_ceil)]
2144+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unsigned_rem_ceil(4), 1);")]
2145+
/// ```
2146+
#[unstable(feature = "rem_ceil", issue = "88581")]
2147+
#[must_use = "this returns the result of the operation, \
2148+
without modifying the original"]
2149+
#[inline]
2150+
#[rustc_inherit_overflow_checks]
2151+
pub const fn unsigned_rem_ceil(self, rhs: Self) -> Self {
2152+
let r = self % rhs;
2153+
if r != 0 {
2154+
rhs - r
2155+
} else {
2156+
r
2157+
}
2158+
}
2159+
20962160
/// Calculates the smallest value greater than or equal to `self` that
20972161
/// is a multiple of `rhs`.
20982162
///

0 commit comments

Comments
 (0)