Skip to content

Commit 48caf28

Browse files
committed
lcm(0, 0) = 0
1 parent fb097cb commit 48caf28

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern crate num_traits as traits;
2424

2525
use core::ops::Add;
2626

27-
use traits::{Num, Signed};
27+
use traits::{Num, Signed, Zero};
2828

2929
mod roots;
3030
pub use roots::Roots;
@@ -92,6 +92,7 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
9292
/// # use num_integer::Integer;
9393
/// assert_eq!(7.lcm(&3), 21);
9494
/// assert_eq!(2.lcm(&4), 4);
95+
/// assert_eq!(0.lcm(&0), 0);
9596
/// ~~~
9697
fn lcm(&self, other: &Self) -> Self;
9798

@@ -293,8 +294,9 @@ macro_rules! impl_integer_for_isize {
293294
/// `other`.
294295
#[inline]
295296
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() }
298300
}
299301

300302
/// Deprecated, use `is_multiple_of` instead.
@@ -557,7 +559,8 @@ macro_rules! impl_integer_for_usize {
557559
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
558560
#[inline]
559561
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)) }
561564
}
562565

563566
/// Deprecated, use `is_multiple_of` instead.

0 commit comments

Comments
 (0)