Skip to content

Commit 1588c84

Browse files
committed
Add rem_floor and rem_ceil
1 parent a48861a commit 1588c84

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

library/core/src/num/int_macros.rs

+81
Original file line numberDiff line numberDiff line change
@@ -3030,6 +3030,45 @@ macro_rules! int_impl {
30303030
}
30313031
}
30323032

3033+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
3034+
///
3035+
/// # Panics
3036+
///
3037+
/// This function will panic if `rhs` is zero.
3038+
///
3039+
/// ## Overflow behavior
3040+
///
3041+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
3042+
/// mode) and wrap if overflow checks are disabled (default in release mode).
3043+
///
3044+
/// # Examples
3045+
///
3046+
/// Basic usage:
3047+
///
3048+
/// ```
3049+
/// #![feature(int_roundings)]
3050+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3051+
/// let b = 3;
3052+
///
3053+
/// assert_eq!(a.rem_floor(b), 2);
3054+
/// assert_eq!(a.rem_floor(-b), -1);
3055+
/// assert_eq!((-a).rem_floor(b), 1);
3056+
/// assert_eq!((-a).rem_floor(-b), -2);
3057+
/// ```
3058+
#[unstable(feature = "int_roundings", issue = "88581")]
3059+
#[must_use = "this returns the result of the operation, \
3060+
without modifying the original"]
3061+
#[inline]
3062+
#[rustc_inherit_overflow_checks]
3063+
pub const fn rem_floor(self, rhs: Self) -> Self {
3064+
let r = self % rhs;
3065+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3066+
r + rhs
3067+
} else {
3068+
r
3069+
}
3070+
}
3071+
30333072
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
30343073
///
30353074
/// # Panics
@@ -3066,6 +3105,48 @@ macro_rules! int_impl {
30663105
}
30673106
}
30683107

3108+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
3109+
///
3110+
/// This operation is *only* available for signed integers,
3111+
/// since the result would be negative if both operands are positive.
3112+
///
3113+
/// # Panics
3114+
///
3115+
/// This function will panic if `rhs` is zero.
3116+
///
3117+
/// ## Overflow behavior
3118+
///
3119+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
3120+
/// mode) and wrap if overflow checks are disabled (default in release mode).
3121+
///
3122+
/// # Examples
3123+
///
3124+
/// Basic usage:
3125+
///
3126+
/// ```
3127+
/// #![feature(rem_ceil)]
3128+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3129+
/// let b = 3;
3130+
///
3131+
/// assert_eq!(a.rem_ceil(b), -1);
3132+
/// assert_eq!(a.rem_ceil(-b), 2);
3133+
/// assert_eq!((-a).rem_ceil(b), -2);
3134+
/// assert_eq!((-a).rem_ceil(-b), 1);
3135+
/// ```
3136+
#[unstable(feature = "rem_ceil", issue = "88581")]
3137+
#[must_use = "this returns the result of the operation, \
3138+
without modifying the original"]
3139+
#[inline]
3140+
#[rustc_inherit_overflow_checks]
3141+
pub const fn rem_ceil(self, rhs: Self) -> Self {
3142+
let r = self % rhs;
3143+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
3144+
r - rhs
3145+
} else {
3146+
r
3147+
}
3148+
}
3149+
30693150
/// If `rhs` is positive, calculates the smallest value greater than or
30703151
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
30713152
/// calculates the largest value less than or equal to `self` that is a

library/core/src/num/uint_macros.rs

+64
Original file line numberDiff line numberDiff line change
@@ -2863,6 +2863,32 @@ macro_rules! uint_impl {
28632863
self / rhs
28642864
}
28652865

2866+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2867+
///
2868+
/// This is the same as performing `self % rhs` for all unsigned integers.
2869+
///
2870+
/// # Panics
2871+
///
2872+
/// This function will panic if `rhs` is zero.
2873+
///
2874+
/// # Examples
2875+
///
2876+
/// Basic usage:
2877+
///
2878+
/// ```
2879+
/// #![feature(int_roundings)]
2880+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".rem_floor(4), 3);")]
2881+
/// ```
2882+
#[unstable(feature = "int_roundings", issue = "88581")]
2883+
#[must_use = "this returns the result of the operation, \
2884+
without modifying the original"]
2885+
#[inline]
2886+
#[rustc_inherit_overflow_checks]
2887+
pub const fn rem_floor(self, rhs: Self) -> Self {
2888+
self % rhs
2889+
}
2890+
2891+
28662892
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
28672893
///
28682894
/// # Panics
@@ -2892,6 +2918,44 @@ macro_rules! uint_impl {
28922918
}
28932919
}
28942920

2921+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2922+
///
2923+
/// Since this remainder can never be positive, we return the opposite of the actual remainder.
2924+
/// If you want the sign to reflect the actual remainder, you need to use the [signed version].
2925+
///
2926+
#[doc = concat!("[signed version]: primitive.", stringify!($SignedT), ".html#method.rem_ceil")]
2927+
///
2928+
/// # Panics
2929+
///
2930+
/// This function will panic if `rhs` is zero.
2931+
///
2932+
/// ## Overflow behavior
2933+
///
2934+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2935+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2936+
///
2937+
/// # Examples
2938+
///
2939+
/// Basic usage:
2940+
///
2941+
/// ```
2942+
/// #![feature(rem_ceil)]
2943+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unsigned_rem_ceil(4), 1);")]
2944+
/// ```
2945+
#[unstable(feature = "rem_ceil", issue = "88581")]
2946+
#[must_use = "this returns the result of the operation, \
2947+
without modifying the original"]
2948+
#[inline]
2949+
#[rustc_inherit_overflow_checks]
2950+
pub const fn unsigned_rem_ceil(self, rhs: Self) -> Self {
2951+
let r = self % rhs;
2952+
if r != 0 {
2953+
rhs - r
2954+
} else {
2955+
r
2956+
}
2957+
}
2958+
28952959
/// Calculates the smallest value greater than or equal to `self` that
28962960
/// is a multiple of `rhs`.
28972961
///

0 commit comments

Comments
 (0)