Skip to content

Commit c704b49

Browse files
committed
Add rem_floor and rem_ceil
1 parent ad12a2a commit c704b49

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
@@ -2906,6 +2906,45 @@ macro_rules! int_impl {
29062906
}
29072907
}
29082908

2909+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2910+
///
2911+
/// # Panics
2912+
///
2913+
/// This function will panic if `rhs` is zero.
2914+
///
2915+
/// ## Overflow behavior
2916+
///
2917+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2918+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2919+
///
2920+
/// # Examples
2921+
///
2922+
/// Basic usage:
2923+
///
2924+
/// ```
2925+
/// #![feature(int_roundings)]
2926+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2927+
/// let b = 3;
2928+
///
2929+
/// assert_eq!(a.rem_floor(b), 2);
2930+
/// assert_eq!(a.rem_floor(-b), -1);
2931+
/// assert_eq!((-a).rem_floor(b), 1);
2932+
/// assert_eq!((-a).rem_floor(-b), -2);
2933+
/// ```
2934+
#[unstable(feature = "int_roundings", issue = "88581")]
2935+
#[must_use = "this returns the result of the operation, \
2936+
without modifying the original"]
2937+
#[inline]
2938+
#[rustc_inherit_overflow_checks]
2939+
pub const fn rem_floor(self, rhs: Self) -> Self {
2940+
let r = self % rhs;
2941+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2942+
r + rhs
2943+
} else {
2944+
r
2945+
}
2946+
}
2947+
29092948
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
29102949
///
29112950
/// # Panics
@@ -2942,6 +2981,48 @@ macro_rules! int_impl {
29422981
}
29432982
}
29442983

2984+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2985+
///
2986+
/// This operation is *only* available for signed integers,
2987+
/// since the result would be negative if both operands are positive.
2988+
///
2989+
/// # Panics
2990+
///
2991+
/// This function will panic if `rhs` is zero.
2992+
///
2993+
/// ## Overflow behavior
2994+
///
2995+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2996+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2997+
///
2998+
/// # Examples
2999+
///
3000+
/// Basic usage:
3001+
///
3002+
/// ```
3003+
/// #![feature(rem_ceil)]
3004+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3005+
/// let b = 3;
3006+
///
3007+
/// assert_eq!(a.rem_ceil(b), -1);
3008+
/// assert_eq!(a.rem_ceil(-b), 2);
3009+
/// assert_eq!((-a).rem_ceil(b), -2);
3010+
/// assert_eq!((-a).rem_ceil(-b), 1);
3011+
/// ```
3012+
#[unstable(feature = "rem_ceil", issue = "88581")]
3013+
#[must_use = "this returns the result of the operation, \
3014+
without modifying the original"]
3015+
#[inline]
3016+
#[rustc_inherit_overflow_checks]
3017+
pub const fn rem_ceil(self, rhs: Self) -> Self {
3018+
let r = self % rhs;
3019+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
3020+
r - rhs
3021+
} else {
3022+
r
3023+
}
3024+
}
3025+
29453026
/// If `rhs` is positive, calculates the smallest value greater than or
29463027
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
29473028
/// 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
@@ -2704,6 +2704,32 @@ macro_rules! uint_impl {
27042704
self / rhs
27052705
}
27062706

2707+
/// Calculates the remainder of `self / rhs` if the quotient is rounded toward negative infinity.
2708+
///
2709+
/// This is the same as performing `self % rhs` for all unsigned integers.
2710+
///
2711+
/// # Panics
2712+
///
2713+
/// This function will panic if `rhs` is zero.
2714+
///
2715+
/// # Examples
2716+
///
2717+
/// Basic usage:
2718+
///
2719+
/// ```
2720+
/// #![feature(int_roundings)]
2721+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".rem_floor(4), 3);")]
2722+
/// ```
2723+
#[unstable(feature = "int_roundings", issue = "88581")]
2724+
#[must_use = "this returns the result of the operation, \
2725+
without modifying the original"]
2726+
#[inline]
2727+
#[rustc_inherit_overflow_checks]
2728+
pub const fn rem_floor(self, rhs: Self) -> Self {
2729+
self % rhs
2730+
}
2731+
2732+
27072733
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
27082734
///
27092735
/// # Panics
@@ -2733,6 +2759,44 @@ macro_rules! uint_impl {
27332759
}
27342760
}
27352761

2762+
/// Calculates the remainder of `self / rhs` if the quotient is rounded towards positive infinity.
2763+
///
2764+
/// Since this remainder can never be positive, we return the opposite of the actual remainder.
2765+
/// If you want the sign to reflect the actual remainder, you need to use the [signed version].
2766+
///
2767+
#[doc = concat!("[signed version]: primitive.", stringify!($SignedT), ".html#method.rem_ceil")]
2768+
///
2769+
/// # Panics
2770+
///
2771+
/// This function will panic if `rhs` is zero.
2772+
///
2773+
/// ## Overflow behavior
2774+
///
2775+
/// On overflow, this function will panic if overflow checks are enabled (default in debug
2776+
/// mode) and wrap if overflow checks are disabled (default in release mode).
2777+
///
2778+
/// # Examples
2779+
///
2780+
/// Basic usage:
2781+
///
2782+
/// ```
2783+
/// #![feature(rem_ceil)]
2784+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unsigned_rem_ceil(4), 1);")]
2785+
/// ```
2786+
#[unstable(feature = "rem_ceil", issue = "88581")]
2787+
#[must_use = "this returns the result of the operation, \
2788+
without modifying the original"]
2789+
#[inline]
2790+
#[rustc_inherit_overflow_checks]
2791+
pub const fn unsigned_rem_ceil(self, rhs: Self) -> Self {
2792+
let r = self % rhs;
2793+
if r != 0 {
2794+
rhs - r
2795+
} else {
2796+
r
2797+
}
2798+
}
2799+
27362800
/// Calculates the smallest value greater than or equal to `self` that
27372801
/// is a multiple of `rhs`.
27382802
///

0 commit comments

Comments
 (0)