[poincare] A (Power of) Unit must not be orphan

This commit is contained in:
Ruben Dashyan
2020-03-18 14:25:13 +01:00
committed by Émilie Feral
parent c12ab3a2e2
commit 9a762f1bf6
2 changed files with 8 additions and 18 deletions

View File

@@ -5,7 +5,6 @@
#include <poincare/constant.h>
#include <poincare/cosine.h>
#include <poincare/division.h>
#include <poincare/float.h>
#include <poincare/horizontal_layout.h>
#include <poincare/infinity.h>
#include <poincare/matrix.h>
@@ -398,6 +397,10 @@ Expression Power::shallowReduce(ExpressionNode::ReductionContext reductionContex
// The exponent must be an Integer
return replaceWithUndefinedInPlace();
}
if (parent().isUninitialized() && base.type() == ExpressionNode::Type::Unit) {
// A Power of Unit must not be orphan
return Multiplication::Builder(Rational::Builder(1), *this);
}
}
}
@@ -979,15 +982,6 @@ Expression Power::shallowBeautify(ExpressionNode::ReductionContext reductionCont
replaceWithInPlace(result);
return result;
}
// Step 4: Force Float(1) in front of an orphan Power of Unit
if (parent().isUninitialized() && childAtIndex(0).type() == ExpressionNode::Type::Unit) {
Multiplication m = Multiplication::Builder(Float<double>::Builder(1.0));
replaceWithInPlace(m);
m.addChildAtIndexInPlace(*this, 1, 1);
return std::move(m);
}
return *this;
}

View File

@@ -1,6 +1,5 @@
#include <poincare/unit.h>
#include <poincare/division.h>
#include <poincare/float.h>
#include <poincare/ieee754.h>
#include <poincare/multiplication.h>
#include <poincare/power.h>
@@ -310,19 +309,16 @@ Expression Unit::shallowReduce(ExpressionNode::ReductionContext reductionContext
Expression multiplier = Power::Builder(Rational::Builder(10), Rational::Builder(prefixMultiplier)).shallowReduce(reductionContext);
result = Multiplication::Builder(multiplier, result).shallowReduce(reductionContext);
}
if (parent().isUninitialized() && result.type() == ExpressionNode::Type::Unit) {
// A Unit must not be orphan
result = Multiplication::Builder(Rational::Builder(1), result);
}
replaceWithInPlace(result);
return result;
}
Expression Unit::shallowBeautify(ExpressionNode::ReductionContext reductionContext) {
Expression ancestor = parent();
// Force Float(1) in front of an orphan Unit
if (ancestor.isUninitialized()) {
Multiplication m = Multiplication::Builder(Float<double>::Builder(1.0));
replaceWithInPlace(m);
m.addChildAtIndexInPlace(*this, 1, 1);
return std::move(m);
}
if (!ancestor.isUninitialized() && ancestor.type() == ExpressionNode::Type::Power) {
ancestor = ancestor.parent();
}