[poincare] DivisionQuotient, DivisionRemainder: add method to reduce on

Integers
This commit is contained in:
Émilie Feral
2019-12-26 10:47:31 +01:00
committed by Léa Saviot
parent de2046fea8
commit ae438ca546
4 changed files with 20 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
#define POINCARE_DIVISION_QUOTIENT_H
#include <poincare/expression.h>
#include <poincare/integer.h>
namespace Poincare {
@@ -44,6 +45,7 @@ public:
// Expression
Expression shallowReduce(Context * context);
static Expression Reduce(const Integer & a, const Integer & b);
};
}

View File

@@ -3,6 +3,7 @@
#include <poincare/approximation_helper.h>
#include <poincare/expression.h>
#include <poincare/integer.h>
namespace Poincare {
@@ -46,6 +47,7 @@ public:
// Expression
Expression shallowReduce(Context * context);
static Expression Reduce(const Integer & a, const Integer & b);
};
}

View File

@@ -68,16 +68,18 @@ Expression DivisionQuotient::shallowReduce(Context * context) {
Integer a = r0.signedIntegerNumerator();
Integer b = r1.signedIntegerNumerator();
Expression result = Reduce(a, b);
replaceWithInPlace(result);
return result;
}
Expression DivisionQuotient::Reduce(const Integer & a, const Integer & b) {
if (b.isZero()) {
Expression result = Infinity::Builder(a.isNegative());
replaceWithInPlace(result);
return result;
return Infinity::Builder(a.isNegative());
}
Integer result = Integer::Division(a, b).quotient;
assert(!result.isOverflow());
Expression rationalResult = Rational::Builder(result);
replaceWithInPlace(rationalResult);
return rationalResult;
return Rational::Builder(result);
}
}

View File

@@ -69,16 +69,18 @@ Expression DivisionRemainder::shallowReduce(Context * context) {
Integer a = r0.signedIntegerNumerator();
Integer b = r1.signedIntegerNumerator();
Expression result = Reduce(a, b);
replaceWithInPlace(result);
return result;
}
Expression DivisionRemainder::Reduce(const Integer & a, const Integer & b) {
if (b.isZero()) {
Expression result = Undefined::Builder();
replaceWithInPlace(result);
return result;
return Undefined::Builder();
}
Integer result = Integer::Division(a, b).remainder;
assert(!result.isOverflow());
Expression rationalResult = Rational::Builder(result);
replaceWithInPlace(rationalResult);
return rationalResult;
return Rational::Builder(result);
}
}