Skip to content

Commit d7600dc

Browse files
committed
Add rem_floor and rem_ceil
1 parent 1db9c06 commit d7600dc

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
@@ -2189,6 +2189,45 @@ macro_rules! int_impl {
21892189
}
21902190
}
21912191

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

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