core: Fix overflow in `int::mod_euc` when `self < 0 && rhs == MIN`
This commit removes usage of `abs`, which overflows when `self == MIN`.
pub fn mod_euc(self, rhs: Self) -> Self {
let r = self % rhs;
if r < 0 {
- r + rhs.abs()
+ if rhs < 0 {
+ r - rhs
+ } else {
+ r + rhs
+ }
} else {
r
}
#![feature(core_private_diy_float)]
#![feature(dec2flt)]
#![feature(decode_utf8)]
+#![feature(euclidean_division)]
#![feature(exact_size_is_empty)]
#![feature(fixed_size_array)]
#![feature(float_internals)]
num::test_num(10 as $T, 2 as $T);
}
+ #[test]
+ fn test_mod_euc() {
+ assert!((-1 as $T).mod_euc(MIN) == MAX);
+ }
+
#[test]
pub fn test_abs() {
assert!((1 as $T).abs() == 1 as $T);