Skip to content

Commit d81a0e9

Browse files
authored
update comment
1 parent aafe6db commit d81a0e9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

library/core/src/num/int_macros.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2068,10 +2068,14 @@ macro_rules! int_impl {
20682068
pub const fn rem_euclid(self, rhs: Self) -> Self {
20692069
let r = self % rhs;
20702070
if r < 0 {
2071-
// if rhs is `integer::MIN`, rhs.wrapping_abs() == rhs.wrapping_abs,
2072-
// thus r.wrapping_add(rhs.wrapping_abs()) == r.wrapping_add(rhs) == r - rhs,
2073-
// which suits our need.
2074-
// otherwise, rhs.wrapping_abs() == -rhs, which won't overflow since r is negative.
2071+
// Semantically equivalent to `if rhs < 0 { r - rhs } else { r + rhs }`.
2072+
// If `rhs` is not `Self::MIN`, then `r + abs(rhs)` will not overflow
2073+
// and is clearly equivalent, because `r` is negative.
2074+
// Otherwise, `rhs` is `Self::MIN`, then we have
2075+
// `r.wrapping_add(Self::MIN.wrapping_abs())`, which evaluates
2076+
// to `r.wrapping_add(Self::MIN)`, which is equivalent to
2077+
// `r - Self::MIN`, which is what we wanted (and will not overflow
2078+
// for negative `r`).
20752079
r.wrapping_add(rhs.wrapping_abs())
20762080
} else {
20772081
r

0 commit comments

Comments
 (0)