Skip to content

Commit adf7d75

Browse files
committed
Add rem_floor and rem_ceil
1 parent c5f69bd commit adf7d75

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
@@ -2825,6 +2825,45 @@ macro_rules! int_impl {
28252825
}
28262826
}
28272827

2828+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2829+
///
2830+
/// # Panics
2831+
///
2832+
/// This function will panic if `rhs` is zero.
2833+
///
2834+
/// ## Overflow behavior
2835+
///
2836+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2837+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2838+
///
2839+
/// # Examples
2840+
///
2841+
/// Basic usage:
2842+
///
2843+
/// ```
2844+
/// #![feature(int_roundings)]
2845+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2846+
/// let b = 3;
2847+
///
2848+
/// assert_eq!(a.rem_floor(b), 2);
2849+
/// assert_eq!(a.rem_floor(-b), -1);
2850+
/// assert_eq!((-a).rem_floor(b), 1);
2851+
/// assert_eq!((-a).rem_floor(-b), -2);
2852+
/// ```
2853+
#[unstable(feature = "int_roundings", issue = "88581")]
2854+
#[must_use = "this returns the result of the operation, \
2855+
without modifying the original"]
2856+
#[inline]
2857+
#[rustc_inherit_overflow_checks]
2858+
pub const fn rem_floor(self, rhs: Self) -> Self {
2859+
let r = self % rhs;
2860+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2861+
r + rhs
2862+
} else {
2863+
r
2864+
}
2865+
}
2866+
28282867
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
28292868
///
28302869
/// # Panics
@@ -2861,6 +2900,48 @@ macro_rules! int_impl {
28612900
}
28622901
}
28632902

2903+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2904+
///
2905+
/// This operation is *only* available for signed integers,
2906+
/// since the result would be negative if both operands are positive.
2907+
///
2908+
/// # Panics
2909+
///
2910+
/// This function will panic if `rhs` is zero.
2911+
///
2912+
/// ## Overflow behavior
2913+
///
2914+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2915+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2916+
///
2917+
/// # Examples
2918+
///
2919+
/// Basic usage:
2920+
///
2921+
/// ```
2922+
/// #![feature(rem_ceil)]
2923+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2924+
/// let b = 3;
2925+
///
2926+
/// assert_eq!(a.rem_ceil(b), -1);
2927+
/// assert_eq!(a.rem_ceil(-b), 2);
2928+
/// assert_eq!((-a).rem_ceil(b), -2);
2929+
/// assert_eq!((-a).rem_ceil(-b), 1);
2930+
/// ```
2931+
#[unstable(feature = "rem_ceil", issue = "88581")]
2932+
#[must_use = "this returns the result of the operation, \
2933+
without modifying the original"]
2934+
#[inline]
2935+
#[rustc_inherit_overflow_checks]
2936+
pub const fn rem_ceil(self, rhs: Self) -> Self {
2937+
let r = self % rhs;
2938+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
2939+
r - rhs
2940+
} else {
2941+
r
2942+
}
2943+
}
2944+
28642945
/// If `rhs` is positive, calculates the smallest value greater than or
28652946
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
28662947
/// 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
@@ -2583,6 +2583,32 @@ macro_rules! uint_impl {
25832583
self / rhs
25842584
}
25852585

2586+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2587+
///
2588+
/// This is the same as performing `self % rhs` for all unsigned integers.
2589+
///
2590+
/// # Panics
2591+
///
2592+
/// This function will panic if `rhs` is zero.
2593+
///
2594+
/// # Examples
2595+
///
2596+
/// Basic usage:
2597+
///
2598+
/// ```
2599+
/// #![feature(int_roundings)]
2600+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".rem_floor(4), 3);")]
2601+
/// ```
2602+
#[unstable(feature = "int_roundings", issue = "88581")]
2603+
#[must_use = "this returns the result of the operation, \
2604+
without modifying the original"]
2605+
#[inline]
2606+
#[rustc_inherit_overflow_checks]
2607+
pub const fn rem_floor(self, rhs: Self) -> Self {
2608+
self % rhs
2609+
}
2610+
2611+
25862612
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
25872613
///
25882614
/// # Panics
@@ -2605,13 +2631,51 @@ macro_rules! uint_impl {
26052631
pub const fn div_ceil(self, rhs: Self) -> Self {
26062632
let d = self / rhs;
26072633
let r = self % rhs;
2608-
if r > 0 && rhs > 0 {
2634+
if r != 0 {
26092635
d + 1
26102636
} else {
26112637
d
26122638
}
26132639
}
26142640

2641+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2642+
///
2643+
/// Since this remainder can never be positive, we return the opposite of the actual remainder.
2644+
/// If you want the sign to reflect the actual remainder, you need to use the [signed version].
2645+
///
2646+
#[doc = concat!("[signed version]: primitive.", stringify!($SignedT), ".html#method.rem_ceil")]
2647+
///
2648+
/// # Panics
2649+
///
2650+
/// This function will panic if `rhs` is zero.
2651+
///
2652+
/// ## Overflow behavior
2653+
///
2654+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2655+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2656+
///
2657+
/// # Examples
2658+
///
2659+
/// Basic usage:
2660+
///
2661+
/// ```
2662+
/// #![feature(rem_ceil)]
2663+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unsigned_rem_ceil(4), 1);")]
2664+
/// ```
2665+
#[unstable(feature = "rem_ceil", issue = "88581")]
2666+
#[must_use = "this returns the result of the operation, \
2667+
without modifying the original"]
2668+
#[inline]
2669+
#[rustc_inherit_overflow_checks]
2670+
pub const fn unsigned_rem_ceil(self, rhs: Self) -> Self {
2671+
let r = self % rhs;
2672+
if r != 0 {
2673+
rhs - r
2674+
} else {
2675+
r
2676+
}
2677+
}
2678+
26152679
/// Calculates the smallest value greater than or equal to `self` that
26162680
/// is a multiple of `rhs`.
26172681
///

0 commit comments

Comments
 (0)