Files
Upsilon/poincare/src/logarithm.cpp
Émilie Feral e951140298 [poincare] Change method createLayout() to createLayout(DisplayMode)
Change-Id: Ifb1027d38b53a50a0ada80e11b68d44e72ac9099
2017-01-31 10:36:13 +01:00

46 lines
1.5 KiB
C++

#include <poincare/logarithm.h>
extern "C" {
#include <assert.h>
#include <math.h>
#include <stdlib.h>
}
#include "layout/horizontal_layout.h"
#include "layout/parenthesis_layout.h"
#include "layout/string_layout.h"
#include "layout/baseline_relative_layout.h"
Logarithm::Logarithm() :
Function("log")
{
}
Expression::Type Logarithm::type() const {
return Type::Logarithm;
}
Expression * Logarithm::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(numberOfOperands == 1 || numberOfOperands == 2);
assert(newOperands != nullptr);
Logarithm * l = new Logarithm();
l->setArgument(newOperands, numberOfOperands, cloneOperands);
return l;
}
float Logarithm::approximate(Context& context) const {
if (m_numberOfArguments == 1) {
return log10f(m_args[0]->approximate(context));
}
return log10f(m_args[1]->approximate(context))/log10f(m_args[0]->approximate(context));
}
ExpressionLayout * Logarithm::createLayout(DisplayMode displayMode) const {
if (m_numberOfArguments == 1) {
return Function::createLayout(displayMode);
}
ExpressionLayout ** childrenLayouts = (ExpressionLayout **)malloc(2*sizeof(ExpressionLayout *));
childrenLayouts[0] = new BaselineRelativeLayout(new StringLayout(m_name, strlen(m_name)), m_args[0]->createLayout(displayMode), BaselineRelativeLayout::Type::Subscript);
childrenLayouts[1] = new ParenthesisLayout(m_args[1]->createLayout(displayMode));
return new HorizontalLayout(childrenLayouts, 2);
}