From d81a0e9e2df9d6a693d7eef362c0aed393e1bf2e Mon Sep 17 00:00:00 2001 From: Neutron3529 Date: Fri, 4 Nov 2022 15:37:33 +0800 Subject: [PATCH] update comment --- library/core/src/num/int_macros.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 36b6d6e774ea..a6deb3f65ec5 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -2068,10 +2068,14 @@ macro_rules! int_impl { pub const fn rem_euclid(self, rhs: Self) -> Self { let r = self % rhs; if r < 0 { - // if rhs is `integer::MIN`, rhs.wrapping_abs() == rhs.wrapping_abs, - // thus r.wrapping_add(rhs.wrapping_abs()) == r.wrapping_add(rhs) == r - rhs, - // which suits our need. - // otherwise, rhs.wrapping_abs() == -rhs, which won't overflow since r is negative. + // Semantically equivalent to `if rhs < 0 { r - rhs } else { r + rhs }`. + // If `rhs` is not `Self::MIN`, then `r + abs(rhs)` will not overflow + // and is clearly equivalent, because `r` is negative. + // Otherwise, `rhs` is `Self::MIN`, then we have + // `r.wrapping_add(Self::MIN.wrapping_abs())`, which evaluates + // to `r.wrapping_add(Self::MIN)`, which is equivalent to + // `r - Self::MIN`, which is what we wanted (and will not overflow + // for negative `r`). r.wrapping_add(rhs.wrapping_abs()) } else { r