@@ -24,7 +24,7 @@ extern crate num_traits as traits;
24
24
25
25
use core:: ops:: Add ;
26
26
27
- use traits:: { Num , Signed } ;
27
+ use traits:: { Num , Signed , Zero } ;
28
28
29
29
mod roots;
30
30
pub use roots:: Roots ;
@@ -92,6 +92,7 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
92
92
/// # use num_integer::Integer;
93
93
/// assert_eq!(7.lcm(&3), 21);
94
94
/// assert_eq!(2.lcm(&4), 4);
95
+ /// assert_eq!(0.lcm(&0), 0);
95
96
/// ~~~
96
97
fn lcm ( & self , other : & Self ) -> Self ;
97
98
@@ -293,8 +294,9 @@ macro_rules! impl_integer_for_isize {
293
294
/// `other`.
294
295
#[ inline]
295
296
fn lcm( & self , other: & Self ) -> Self {
296
- // should not have to recalculate abs
297
- ( * self * ( * other / self . gcd( other) ) ) . abs( )
297
+ if self . is_zero( ) && other. is_zero( ) { Self :: zero( ) }
298
+ else { // should not have to recalculate abs
299
+ ( * self * ( * other / self . gcd( other) ) ) . abs( ) }
298
300
}
299
301
300
302
/// Deprecated, use `is_multiple_of` instead.
@@ -557,7 +559,8 @@ macro_rules! impl_integer_for_usize {
557
559
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
558
560
#[ inline]
559
561
fn lcm( & self , other: & Self ) -> Self {
560
- * self * ( * other / self . gcd( other) )
562
+ if self . is_zero( ) && other. is_zero( ) { Self :: zero( ) }
563
+ else { * self * ( * other / self . gcd( other) ) }
561
564
}
562
565
563
566
/// Deprecated, use `is_multiple_of` instead.
0 commit comments