square_root.cpp
1.16 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
#include <poincare/square_root.h>
#include <poincare/complex.h>
#include <poincare/power.h>
#include "layout/nth_root_layout.h"
extern "C" {
#include <assert.h>
}
#include <cmath>
namespace Poincare {
SquareRoot::SquareRoot() :
Function("squareRoot")
{
}
Expression::Type SquareRoot::type() const {
return Type::SquareRoot;
}
Expression * SquareRoot::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(newOperands != nullptr);
SquareRoot * sr = new SquareRoot();
sr->setArgument(newOperands, numberOfOperands, cloneOperands);
return sr;
}
ExpressionLayout * SquareRoot::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
assert(floatDisplayMode != FloatDisplayMode::Default);
assert(complexFormat != ComplexFormat::Default);
return new NthRootLayout(m_args[0]->createLayout(floatDisplayMode, complexFormat),nullptr);
}
template<typename T>
Complex<T> SquareRoot::templatedComputeComplex(const Complex<T> c) const {
if (c.b() == 0 && c.a() >= 0) {
return Complex<T>::Float(std::sqrt(c.a()));
}
return Power::compute(c, Complex<T>::Float(0.5));
}
}