[poincare/arithmetic] Shortcut the LCM and GCD computation if equal

This fixes: LCM(7.88861e+169, 7.88861e+169), which overflowed at some
point during the computation
This commit is contained in:
Léa Saviot
2020-03-12 16:55:18 +01:00
committed by RubenNumworks
parent 97d94d9e56
commit 313cbf6767

View File

@@ -7,6 +7,9 @@ Integer Arithmetic::LCM(const Integer & a, const Integer & b) {
if (a.isZero() || b.isZero()) {
return Integer(0);
}
if (a.isEqualTo(b)) {
return a;
}
Integer signResult = Integer::Division(Integer::Multiplication(a, b), GCD(a, b)).quotient;
signResult.setNegative(false);
return signResult;
@@ -16,7 +19,9 @@ Integer Arithmetic::GCD(const Integer & a, const Integer & b) {
if (a.isOverflow() || b.isOverflow()) {
return Integer::Overflow(false);
}
if (a.isEqualTo(b)) {
return a;
}
Integer i = a;
Integer j = b;
i.setNegative(false);