Numworks Epsilon  1.4.1
Graphing Calculator Operating System
cosine.cpp
Go to the documentation of this file.
1 #include <poincare/cosine.h>
3 #include <poincare/complex.h>
4 #include <poincare/symbol.h>
5 #include <poincare/rational.h>
8 #include <ion.h>
9 extern "C" {
10 #include <assert.h>
11 }
12 #include <cmath>
13 
14 namespace Poincare {
15 
17  return Type::Cosine;
18 }
19 
21  Cosine * a = new Cosine(m_operands, true);
22  return a;
23 }
24 
25 float Cosine::characteristicXRange(Context & context, AngleUnit angleUnit) const {
26  return Trigonometry::characteristicXRange(this, context, angleUnit);
27 }
28 
29 Expression * Cosine::shallowReduce(Context& context, AngleUnit angleUnit) {
30  Expression * e = Expression::shallowReduce(context, angleUnit);
31  if (e != this) {
32  return e;
33  }
34 #if MATRIX_EXACT_REDUCING
35  Expression * op = editableOperand(0);
36  if (op->type() == Type::Matrix) {
37  return SimplificationEngine::map(this, context, angleUnit);
38  }
39 #endif
40  return Trigonometry::shallowReduceDirectFunction(this, context, angleUnit);
41 }
42 
43 template<typename T>
44 Complex<T> Cosine::computeOnComplex(const Complex<T> c, AngleUnit angleUnit) {
45  assert(angleUnit != AngleUnit::Default);
46  if (c.b() == 0) {
47  T input = c.a();
48  if (angleUnit == AngleUnit::Degree) {
49  input *= M_PI/180.0f;
50  }
51  T result = std::cos(input);
52  /* Cheat: openbsd trigonometric functions (cos, sin & tan) are numerical
53  * implementation and thus are approximative. The error epsilon is ~1E-7
54  * on float and ~1E-15 on double. In order to avoid weird results as
55  * cos(90) = 6E-17, we neglect the result when its ratio with the argument
56  * (pi in the exemple) is smaller than epsilon.
57  * We can't do that for all evaluation as the user can operate on values as
58  * small as 1E-308 (in double) and most results still be correct. */
59  if (input != 0 && std::fabs(result/input) <= epsilon<T>()) {
60  return Complex<T>::Float(0);
61  }
62  return Complex<T>::Float(result);
63  }
64  Complex<T> arg = Complex<T>::Cartesian(-c.b(), c.a());
65  return HyperbolicCosine::computeOnComplex(arg, angleUnit);
66 }
67 
68 }
static float characteristicXRange(const Expression *e, Context &context, Expression::AngleUnit angleUnit)
static Complex< T > Cartesian(T a, T b)
Definition: complex.cpp:28
#define assert(e)
Definition: assert.h:9
#define M_PI
Definition: math.h:17
#define T(x)
Definition: events.cpp:26
Expression * clone() const override
Definition: cosine.cpp:20
#define fabs(x)
Definition: math.h:178
constexpr Event Cosine
Definition: events.h:86
c(generic_all_nodes)
#define cos(x)
Definition: math.h:172
static Expression * shallowReduceDirectFunction(Expression *e, Context &context, Expression::AngleUnit angleUnit)
static Complex< T > Float(T x)
Definition: complex.cpp:23
Type type() const override
Definition: cosine.cpp:16
static Complex< T > computeOnComplex(const Complex< T > c, AngleUnit angleUnit=AngleUnit::Radian)
Definition: cosine.cpp:44
constexpr Poincare::Expression::AngleUnit Degree
Definition: helper.h:3
static Complex< T > computeOnComplex(const Complex< T > c, AngleUnit angleUnit)
float characteristicXRange(Context &context, AngleUnit angleUnit=AngleUnit::Default) const override
Definition: cosine.cpp:25