Skip to content

Commit 0da93e6

Browse files
committed
Add rem_floor and rem_ceil
1 parent 130ff8c commit 0da93e6

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
@@ -2244,6 +2244,45 @@ macro_rules! int_impl {
22442244
}
22452245
}
22462246

2247+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2248+
///
2249+
/// # Panics
2250+
///
2251+
/// This function will panic if `rhs` is zero.
2252+
///
2253+
/// ## Overflow behavior
2254+
///
2255+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2256+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2257+
///
2258+
/// # Examples
2259+
///
2260+
/// Basic usage:
2261+
///
2262+
/// ```
2263+
/// #![feature(int_roundings)]
2264+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2265+
/// let b = 3;
2266+
///
2267+
/// assert_eq!(a.rem_floor(b), 2);
2268+
/// assert_eq!(a.rem_floor(-b), -1);
2269+
/// assert_eq!((-a).rem_floor(b), 1);
2270+
/// assert_eq!((-a).rem_floor(-b), -2);
2271+
/// ```
2272+
#[unstable(feature = "int_roundings", issue = "88581")]
2273+
#[must_use = "this returns the result of the operation, \
2274+
without modifying the original"]
2275+
#[inline]
2276+
#[rustc_inherit_overflow_checks]
2277+
pub const fn rem_floor(self, rhs: Self) -> Self {
2278+
let r = self % rhs;
2279+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2280+
r + rhs
2281+
} else {
2282+
r
2283+
}
2284+
}
2285+
22472286
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
22482287
///
22492288
/// # Panics
@@ -2284,6 +2323,48 @@ macro_rules! int_impl {
22842323
}
22852324
}
22862325

2326+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2327+
///
2328+
/// This operation is *only* available for signed integers,
2329+
/// since the result would be negative if both operands are positive.
2330+
///
2331+
/// # Panics
2332+
///
2333+
/// This function will panic if `rhs` is zero.
2334+
///
2335+
/// ## Overflow behavior
2336+
///
2337+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2338+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2339+
///
2340+
/// # Examples
2341+
///
2342+
/// Basic usage:
2343+
///
2344+
/// ```
2345+
/// #![feature(rem_ceil)]
2346+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2347+
/// let b = 3;
2348+
///
2349+
/// assert_eq!(a.rem_ceil(b), -1);
2350+
/// assert_eq!(a.rem_ceil(-b), 2);
2351+
/// assert_eq!((-a).rem_ceil(b), -2);
2352+
/// assert_eq!((-a).rem_ceil(-b), 1);
2353+
/// ```
2354+
#[unstable(feature = "rem_ceil", issue = "88581")]
2355+
#[must_use = "this returns the result of the operation, \
2356+
without modifying the original"]
2357+
#[inline]
2358+
#[rustc_inherit_overflow_checks]
2359+
pub const fn rem_ceil(self, rhs: Self) -> Self {
2360+
let r = self % rhs;
2361+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
2362+
r - rhs
2363+
} else {
2364+
r
2365+
}
2366+
}
2367+
22872368
/// If `rhs` is positive, calculates the smallest value greater than or
22882369
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
22892370
/// 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
@@ -2123,6 +2123,32 @@ macro_rules! uint_impl {
21232123
self / rhs
21242124
}
21252125

2126+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2127+
///
2128+
/// This is the same as performing `self % rhs` for all unsigned integers.
2129+
///
2130+
/// # Panics
2131+
///
2132+
/// This function will panic if `rhs` is zero.
2133+
///
2134+
/// # Examples
2135+
///
2136+
/// Basic usage:
2137+
///
2138+
/// ```
2139+
/// #![feature(int_roundings)]
2140+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".rem_floor(4), 3);")]
2141+
/// ```
2142+
#[unstable(feature = "int_roundings", issue = "88581")]
2143+
#[must_use = "this returns the result of the operation, \
2144+
without modifying the original"]
2145+
#[inline]
2146+
#[rustc_inherit_overflow_checks]
2147+
pub const fn rem_floor(self, rhs: Self) -> Self {
2148+
self % rhs
2149+
}
2150+
2151+
21262152
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
21272153
///
21282154
/// # Panics
@@ -2150,13 +2176,51 @@ macro_rules! uint_impl {
21502176
pub const fn div_ceil(self, rhs: Self) -> Self {
21512177
let d = self / rhs;
21522178
let r = self % rhs;
2153-
if r > 0 && rhs > 0 {
2179+
if r != 0 {
21542180
d + 1
21552181
} else {
21562182
d
21572183
}
21582184
}
21592185

2186+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2187+
///
2188+
/// Since this remainder can never be positive, we return the opposite of the actual remainder.
2189+
/// If you want the sign to reflect the actual remainder, you need to use the [signed version].
2190+
///
2191+
#[doc = concat!("[signed version]: primitive.", stringify!($SignedT), ".html#method.rem_ceil")]
2192+
///
2193+
/// # Panics
2194+
///
2195+
/// This function will panic if `rhs` is zero.
2196+
///
2197+
/// ## Overflow behavior
2198+
///
2199+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2200+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2201+
///
2202+
/// # Examples
2203+
///
2204+
/// Basic usage:
2205+
///
2206+
/// ```
2207+
/// #![feature(rem_ceil)]
2208+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unsigned_rem_ceil(4), 1);")]
2209+
/// ```
2210+
#[unstable(feature = "rem_ceil", issue = "88581")]
2211+
#[must_use = "this returns the result of the operation, \
2212+
without modifying the original"]
2213+
#[inline]
2214+
#[rustc_inherit_overflow_checks]
2215+
pub const fn unsigned_rem_ceil(self, rhs: Self) -> Self {
2216+
let r = self % rhs;
2217+
if r != 0 {
2218+
rhs - r
2219+
} else {
2220+
r
2221+
}
2222+
}
2223+
21602224
/// Calculates the smallest value greater than or equal to `self` that
21612225
/// is a multiple of `rhs`.
21622226
///

0 commit comments

Comments
 (0)