core: Fix overflow in int::mod_euc
when self < 0 && rhs == MIN
This commit is contained in:
parent
1fac3ca9c4
commit
fbb1c280bf
3 changed files with 11 additions and 1 deletions
|
@ -1765,7 +1765,11 @@ assert_eq!((-a).mod_euc(-b), 1);
|
||||||
pub fn mod_euc(self, rhs: Self) -> Self {
|
pub fn mod_euc(self, rhs: Self) -> Self {
|
||||||
let r = self % rhs;
|
let r = self % rhs;
|
||||||
if r < 0 {
|
if r < 0 {
|
||||||
r + rhs.abs()
|
if rhs.is_negative() {
|
||||||
|
r - rhs
|
||||||
|
} else {
|
||||||
|
r + rhs
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#![feature(core_private_diy_float)]
|
#![feature(core_private_diy_float)]
|
||||||
#![feature(dec2flt)]
|
#![feature(dec2flt)]
|
||||||
#![feature(decode_utf8)]
|
#![feature(decode_utf8)]
|
||||||
|
#![feature(euclidean_division)]
|
||||||
#![feature(exact_size_is_empty)]
|
#![feature(exact_size_is_empty)]
|
||||||
#![feature(fixed_size_array)]
|
#![feature(fixed_size_array)]
|
||||||
#![feature(float_internals)]
|
#![feature(float_internals)]
|
||||||
|
|
|
@ -30,6 +30,11 @@ mod tests {
|
||||||
num::test_num(10 as $T, 2 as $T);
|
num::test_num(10 as $T, 2 as $T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mod_euc() {
|
||||||
|
assert!((-1 as $T).mod_euc(MIN) == MAX);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_abs() {
|
pub fn test_abs() {
|
||||||
assert!((1 as $T).abs() == 1 as $T);
|
assert!((1 as $T).abs() == 1 as $T);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue