mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-29 19:49:58 +02:00
[poincare] implement Round::shallowReduce for rationals
Change-Id: I552570bceed2b52dbf6db9b6d47979e0f87b7a4f
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <poincare/round.h>
|
||||
#include <poincare/undefined.h>
|
||||
#include <poincare/rational.h>
|
||||
|
||||
extern "C" {
|
||||
#include <assert.h>
|
||||
@@ -27,6 +28,22 @@ Expression * Round::shallowReduce(Context& context, AngleUnit angleUnit) {
|
||||
return replaceWith(new Undefined(), true);
|
||||
}
|
||||
#endif
|
||||
if (operand(0)->type() == Type::Rational && operand(1)->type() == Type::Rational) {
|
||||
Rational * r1 = static_cast<Rational *>(editableOperand(0));
|
||||
Rational * r2 = static_cast<Rational *>(editableOperand(1));
|
||||
if (!r2->denominator().isOne()) {
|
||||
return replaceWith(new Undefined(), true);
|
||||
}
|
||||
Rational err = Rational::Power(Rational(10), r2->numerator());
|
||||
Rational mult = Rational::Multiplication(*r1, Rational(err));
|
||||
IntegerDivision d = Integer::Division(mult.numerator(), mult.denominator());
|
||||
Integer rounding = d.quotient;
|
||||
if (Rational::NaturalOrder(Rational(d.remainder, mult.denominator()), Rational(1,2)) >= 0) {
|
||||
rounding = Integer::Addition(rounding, Integer(1));
|
||||
}
|
||||
Rational result = Rational::Multiplication(rounding, Rational::Power(Rational(1,10), r2->numerator()));
|
||||
return replaceWith(new Rational(result), true);
|
||||
}
|
||||
return this; // TODO: implement for rationals!
|
||||
}
|
||||
|
||||
|
||||
@@ -149,6 +149,11 @@ QUIZ_CASE(poincare_simplify_easy) {
|
||||
assert_parsed_expression_simplify_to("lcm(123,278)", "34194");
|
||||
assert_parsed_expression_simplify_to("lcm(11,121)", "121");
|
||||
assert_parsed_expression_simplify_to("root(4,3)", "4^(1/3)");
|
||||
assert_parsed_expression_simplify_to("round(4.235,2)", "4.24");
|
||||
assert_parsed_expression_simplify_to("round(4.23,0)", "4");
|
||||
assert_parsed_expression_simplify_to("round(4.9,0)", "5");
|
||||
assert_parsed_expression_simplify_to("round(12.9,-1)", "10");
|
||||
assert_parsed_expression_simplify_to("round(12.9,-2)", "0");
|
||||
assert_parsed_expression_simplify_to("permute(99,4)", "90345024");
|
||||
assert_parsed_expression_simplify_to("permute(20,-10)", "undef");
|
||||
assert_parsed_expression_simplify_to("re(1/2)", "1/2");
|
||||
|
||||
Reference in New Issue
Block a user