round.cpp
1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <poincare/round.h>
extern "C" {
#include <assert.h>
}
#include <cmath>
namespace Poincare {
Round::Round() :
Function("round", 2)
{
}
Expression::Type Round::type() const {
return Type::Round;
}
Expression * Round::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(newOperands != nullptr);
Round * r = new Round();
r->setArgument(newOperands, numberOfOperands, cloneOperands);
return r;
}
template<typename T>
Evaluation<T> * Round::templatedEvaluate(Context& context, AngleUnit angleUnit) const {
Evaluation<T> * f1Entry = m_args[0]->evaluate<T>(context, angleUnit);
Evaluation<T> * f2Entry = m_args[1]->evaluate<T>(context, angleUnit);
T f1 = f1Entry->toScalar();
T f2 = f2Entry->toScalar();
delete f1Entry;
delete f2Entry;
if (std::isnan(f2) || f2 != (int)f2) {
return new Complex<T>(Complex<T>::Float(NAN));
}
T err = std::pow(10, std::floor(f2));
return new Complex<T>(Complex<T>::Float(std::round(f1*err)/err));
}
}