[poincare] Fix division by zero

Change-Id: I644b70b58e6c2610681141158892668afc5a9c58
This commit is contained in:
Émilie Feral
2017-11-20 16:15:41 +01:00
parent 8b2e8c29de
commit 002fc568de
4 changed files with 11 additions and 0 deletions

View File

@@ -6,6 +6,9 @@ namespace Poincare {
const Integer Arithmetic::k_primorial32("525896479052627740771371797072411912900610967452630");
Integer Arithmetic::LCM(const Integer * a, const Integer * b) {
if (a->isZero() || b->isZero()) {
return Integer(0);
}
Integer signResult = Integer::Division(Integer::Multiplication(*a, *b), GCD(a,b)).quotient;
signResult.setNegative(false);
return signResult;

View File

@@ -50,6 +50,9 @@ Expression * DivisionQuotient::shallowReduce(Context& context, AngleUnit angleUn
Integer a = r0->numerator();
Integer b = r1->numerator();
if (b.isZero()) {
return replaceWith(new Undefined(), true); // TODO: new Infinite(a.isNegative())
}
Integer result = Integer::Division(a, b).quotient;
return replaceWith(new Rational(result), true);
}

View File

@@ -50,6 +50,9 @@ Expression * DivisionRemainder::shallowReduce(Context& context, AngleUnit angleU
Integer a = r0->numerator();
Integer b = r1->numerator();
if (b.isZero()) {
return replaceWith(new Undefined(), true); // TODO: new Infinite(a.isNegative())
}
Integer result = Integer::Division(a, b).remainder;
return replaceWith(new Rational(result), true);
}

View File

@@ -136,9 +136,11 @@ QUIZ_CASE(poincare_simplify_easy) {
assert_parsed_expression_simplify_to("ceil(-1.3)", "-1");
assert_parsed_expression_simplify_to("conj(1/2)", "1/2");
assert_parsed_expression_simplify_to("quo(19,3)", "6");
assert_parsed_expression_simplify_to("quo(19,0)", "undef");
assert_parsed_expression_simplify_to("quo(-19,3)", "-7");
assert_parsed_expression_simplify_to("rem(19,3)", "1");
assert_parsed_expression_simplify_to("rem(-19,3)", "2");
assert_parsed_expression_simplify_to("rem(19,0)", "undef");
assert_parsed_expression_simplify_to("99!", "933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000");
assert_parsed_expression_simplify_to("floor(-1.3)", "-2");
assert_parsed_expression_simplify_to("frac(-1.3)", "0.7");