Files
Upsilon/poincare/src/absolute_value.cpp
Émilie Feral 7fee8daccb [poincare] Complete implementation of AbsoluteValue::immediateSimplify
Change-Id: Iedc4d680ad8861932af96a5ec6f3a2289db9f346
2017-10-25 18:18:58 +02:00

45 lines
1.2 KiB
C++

#include <poincare/absolute_value.h>
#include <poincare/complex.h>
#include "layout/absolute_value_layout.h"
extern "C" {
#include <assert.h>
}
#include <cmath>
namespace Poincare {
Expression::Type AbsoluteValue::type() const {
return Type::AbsoluteValue;
}
Expression * AbsoluteValue::clone() const {
AbsoluteValue * a = new AbsoluteValue(m_operands, true);
return a;
}
Expression * AbsoluteValue::immediateSimplify() {
if (operand(0)->sign() > 0) {
return replaceWith(const_cast<Expression *>(operand(0)), true);
}
if (operand(0)->sign() < 0) {
Expression * op = const_cast<Expression *>(operand(0));
Expression * newOp = op->turnIntoPositive();
return replaceWith(newOp, true);
}
return this;
}
template<typename T>
Complex<T> AbsoluteValue::computeOnComplex(const Complex<T> c, AngleUnit angleUnit) {
return Complex<T>::Float(c.r());
}
ExpressionLayout * AbsoluteValue::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
assert(floatDisplayMode != FloatDisplayMode::Default);
assert(complexFormat != ComplexFormat::Default);
return new AbsoluteValueLayout(operand(0)->createLayout(floatDisplayMode, complexFormat));
}
}